# Is it possible to uniquely identify computing threads?

**URL:** https://discourse.julialang.org/t/is-it-possible-to-uniquely-identify-computing-threads/110084
**Category:** General Usage
**Created:** [February 11, 2024, 11:46pm UTC](https://discourse.julialang.org/t/is-it-possible-to-uniquely-identify-computing-threads/110084 "2024-02-11T23:46:54Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [February 11, 2024, 11:46pm UTC](https://discourse.julialang.org/t/is-it-possible-to-uniquely-identify-computing-threads/110084/1 "2024-02-11T23:46:54Z")

</div>

Let us say I spin up a certain number of threads, `julia -t 5`. Is it possible to uniquely identify which thread is running my code at some point in time? I don’t mean `threadid()`, unless that is guaranteed to be unique.

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [February 12, 2024, 5:40am UTC](https://discourse.julialang.org/t/is-it-possible-to-uniquely-identify-computing-threads/110084/2 "2024-02-12T05:40:59Z")

</div>

I am confused what you ask for if `threadid` is not the answer. `threadid` gives you the ID of the thread that currently runs your code. This is not guaranteed to be fixed so calling `threadid` again later might give a different answer because your code is executed by another thread then.

What do you mean by “_uniquely_ identify the thread that is running my code at some point in time”? And why is `threadid` not the answer?

---

<div class="post-metadata">

### Author: ![simsurace](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simsurace/32/30216_2.png) [@simsurace](https://discourse.julialang.org/u/simsurace)
#### Post date: [February 12, 2024, 7:17am UTC](https://discourse.julialang.org/t/is-it-possible-to-uniquely-identify-computing-threads/110084/3 "2024-02-12T07:17:05Z")

</div>

`threadid` does give you the thread at any point in time. It is just wrong to assume that it will be fixed/constant throughout the lifetime of the task.

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [February 12, 2024, 7:45am UTC](https://discourse.julialang.org/t/is-it-possible-to-uniquely-identify-computing-threads/110084/4 "2024-02-12T07:45:55Z")

</div>

> **[PSA: Thread-local state is no longer recommended](https://julialang.org/blog/2023/07/PSA-dont-use-threadid/)**
>
> PSA: Thread-local state is no longer recommended; Common misconceptions about threadid() and nthreads()

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [February 12, 2024, 2:59pm UTC](https://discourse.julialang.org/t/is-it-possible-to-uniquely-identify-computing-threads/110084/5 "2024-02-12T14:59:24Z")

</div>

Right, I know that `threadid()` executing my code can change (thanks!).  
What I was not sure about was: I start 5 threads, but I start only 4 tasks by spawn. I want to know which threads the tasks are running on (in the sense of statistics).

So, I think you guys are saying the threads are given ids when spun up, and those will not change arbitrarily?

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [February 12, 2024, 3:29pm UTC](https://discourse.julialang.org/t/is-it-possible-to-uniquely-identify-computing-threads/110084/6 "2024-02-12T15:29:34Z")

</div>

It seems the question is now moot. See [Surprising behavior for default/interactive thread pools · Issue #53217 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/53217#issuecomment-1938406957)

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [February 12, 2024, 4:02pm UTC](https://discourse.julialang.org/t/is-it-possible-to-uniquely-identify-computing-threads/110084/7 "2024-02-12T16:02:09Z")

</div>

> [@PetrKryslUCSD](#):
>
> So, I think you guys are saying the threads are given ids when spun up, and those will not change arbitrarily?

Yes, Julia threads have IDs that don’t change and `threadid()` is the function to query the ID of the thread that is **currently** running a task.

> [@PetrKryslUCSD](#):
>
> I start 5 threads, but I start only 4 tasks by spawn. I want to know which threads the tasks are running on (in the sense of statistics).

As you know, by default, tasks can migrate between threads. Hence, there is no naive task-\>thread mapping because different parts of a task might run on different threads. Of course, you can opt out of task migration with, e.g., `OhMyThreads.@spawnat` or `@threads :static` to “solve” this. Another way is to track the migration of the task, e.g. by calling `threadid()` multiple times (basically sampling the current threadid) and storing the results for each task. To this end, something like [`OhMyThreads.Tools.taskid()`](https://juliafolds2.github.io/OhMyThreads.jl/stable/refs/internal/#OhMyThreads.Tools.taskid-Tuple%7B%7D) could be useful to (almost certainly uniquely) identify the task. It essentially is a task-pendant for `threadid()`.

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [February 12, 2024, 4:03pm UTC](https://discourse.julialang.org/t/is-it-possible-to-uniquely-identify-computing-threads/110084/8 "2024-02-12T16:03:19Z")

</div>

> [@PetrKryslUCSD](#):
>
> It seems the question is now moot. See [Surprising behavior for default/interactive thread pools · Issue #53217 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/53217#issuecomment-1938406957)

I don’t see how this issue is relevant for the question here. The issue is about the thread pool of the main thread not thread ids.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [February 12, 2024, 5:59pm UTC](https://discourse.julialang.org/t/is-it-possible-to-uniquely-identify-computing-threads/110084/9 "2024-02-12T17:59:59Z")

</div>

Because the answers in that issue will probably have a bearing on what I wanted to investigate with the thread id statistics.
