Julia Lang Advises Against Thread-Local State Due to Concurrency Risks
The Julia programming language team has issued a public service announcement declaring that using thread-local state via threadid() is no longer recommended. This practice, previously suggested in official documentation, contains inherent race conditions because tasks spawned by @threads can yield execution, allowing other tasks on the same thread to overwrite shared state concurrently. This bug persists even in single-threaded environments when multiple tasks are scheduled. The article identifies this as a critical concurrency issue rather than just a multithreading problem. To address this, developers are urged to refactor existing code. A quick but limited fix involves replacing @threads with @threads :static, which prevents task migration but lacks composability. The preferred long-term solution is to manage parallelism directly using @spawn, allowing tasks to handle and return their own state safely. Alternatively, developers can use specialized packages designed to handle these concurrency patterns correctly. This update aims to enhance the stability and correctness of the Julia ecosystem by eliminating reliance on implementation details that lead to data loss and incorrect results in parallel computations.
Wire timeline
Julia Lang Advises Against Thread-Local State Due to Concurrency Risks
The Julia programming language team has issued a public service announcement declaring that using thread-local state via threadid() is no longer recommended. This practice, previously suggested in official documentation, contains inherent race conditions because tasks spawned by @threads can yield execution, allowing other tasks on the same thread to overwrite shared state concurrently. This bug persists even in single-threaded environments when multiple tasks are scheduled. The article identifies this as a critical concurrency issue rather than just a multithreading problem. To address this, developers are urged to refactor existing code. A quick but limited fix involves replacing @threads with @threads :static, which prevents task migration but lacks composability. The preferred long-term solution is to manage parallelism directly using @spawn, allowing tasks to handle and return their own state safely. Alternatively, developers can use specialized packages designed to handle these concurrency patterns correctly. This update aims to enhance the stability and correctness of the Julia ecosystem by eliminating reliance on implementation details that lead to data loss and incorrect results in parallel computations.
JuliaLang - The Julia programming language