Skip to content

Commit

Permalink
[docs] add danger admonition for Threads.threadid
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Jan 8, 2025
1 parent 7b43657 commit 042e429
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/src/tutorials/algorithms/parallelism.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ julia> ids
3
````

!!! danger
The `Threads.threadid()` that a task runs on may change during execution.
Therefore, it is not safe to use `Threads.threadid()` to index into, say, a
vector of buffer or stateful objects. As an example, do not do:
```julia
x = rand(Threads.nthreads())
Threads.@threads for i in 1:Threads.nthreads()
x[Threads.threadid()] *= 2 # Danger! This use of threadid is not safe
end
```
For more information, read
[PSA: Thread-local state is no longer recommended](https://julialang.org/blog/2023/07/PSA-dont-use-threadid/).

Check warning on line 88 in docs/src/tutorials/algorithms/parallelism.md

View workflow job for this annotation

GitHub Actions / build

[vale] reported by reviewdog 🐶 [Google.Colons] ': T' should be in lowercase. Raw Output: {"message": "[Google.Colons] ': T' should be in lowercase.", "location": {"path": "docs/src/tutorials/algorithms/parallelism.md", "range": {"start": {"line": 88, "column": 9}}}, "severity": "WARNING"}

### Thread safety

When working with threads, you must avoid race conditions, in which two
Expand Down

0 comments on commit 042e429

Please sign in to comment.