# Sharp edge with \`Threads.threadid()\` and task migration

**URL:** https://discourse.julialang.org/t/sharp-edge-with-threads-threadid-and-task-migration/124550
**Category:** General Usage
**Tags:** multithreading
**Created:** [January 8, 2025, 5:32pm UTC](https://discourse.julialang.org/t/sharp-edge-with-threads-threadid-and-task-migration/124550 "2025-01-08T17:32:21Z")
**Posts on this page:** 1
**Showing post:** 29

<div class="post-metadata">

### Author: ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)
#### Post date: [January 9, 2025, 1:24pm UTC](https://discourse.julialang.org/t/sharp-edge-with-threads-threadid-and-task-migration/124550/29 "2025-01-09T13:24:03Z")

</div>

This is a good compromise! Even less breaking, while still allowing people to use `threadid()` if they know what they are doing. Completely safe with the previously officially-recommended use of it.

e.g.,

```julia
os_threadid() = #= current behavior of threadid() =#

function threadid()
    if !current_task.sticky
        error(
            "You used `threadid()` in a non-sticky task. " *
            "This is no longer supported as its abuse frequently results in silent cache corruption. " *
            "If you meant to use this, please update to `Threads.os_threadid()`. " *
            "Alternatively, use sticky threads: `Threads.@threads :sticky`."
        )
    end
    return os_threadid() # Explicit call preferred, but allowed if sticky
end

```

Is there a possibility this won’t immediately get shot down if I open to a PR for this? Happy to do so and have triage consider it. @Oscar_Smith @mbauman

---

_[View the full topic](https://discourse.julialang.org/t/sharp-edge-with-threads-threadid-and-task-migration/124550)._
