Skip to content

Commit

Permalink
Add rule for indexing by threadid()
Browse files Browse the repository at this point in the history
  • Loading branch information
iuliadmtru committed Jul 6, 2023
1 parent 6dd2ae0 commit e33bd87
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
28 changes: 28 additions & 0 deletions threads/index_by_threadid.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Base.Threads

states = zeros(10)
Threads.@spawn for i = 1:10
# ruleid: index-by-threadid
tid = Threads.threadid()
old_val = states[tid]
new_val = old_val + i
states[tid] = new_val
end

@spawn begin
# ruleid: index-by-threadid
tid = threadid()
val = states[tid]
states[tid] = val + 1
end

Threads.@spawn for i = 1:10
# ruleid: index-by-threadid
val = states[threadid()]
end


Threads.@spawn for i = 1:10
# ruleid: index-by-threadid
val = states[Threads.threadid()]
end
21 changes: 21 additions & 0 deletions threads/index_by_threadid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
rules:
- id: index-by-threadid
pattern-either:
- pattern: |
$TID = Threads.threadid()
...
$X = $S[$TID]
- pattern: |
$TID = threadid()
...
$X = $S[$TID]
- pattern: $S[threadid()]
- pattern: $S[Threads.threadid()]
message: Indexing by `threadid()` may cause race conditions and should be avoided.
metadata:
references:
- https://julialang.netlify.app/previews/pr1914/blog/2023/06/psa-dont-use-threadid/
license: LGPL
languages:
- julia
severity: WARNING

0 comments on commit e33bd87

Please sign in to comment.