Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] add danger admonition for Threads.threadid #3909

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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
Loading