thread::id() Names the Calling Thread with an Opaque thread::IdStatus: Accepted
Date: November 18, 2026
Answers: how a test sees that two calls ran on the same thread, which
“Unobservable std tests” (#2088) found no way to say in Nikaia for the
library’s kept threads (ADR-284 D9)
Related: ADR-284 D9 (a library’s _async calls run on kept
threads); ADR-400 (std::mem counts per thread; an address
of a type that allows only == was refused there)
// a C program calls the library's `work_async` twice, one after the other
pub fn work() -> int {
// did the second call run on the thread the first one used?
}
std’s Rust test compares std::thread::current().id() of the two calls. A
Nikaia program has no way to name the thread it runs on.
| Option | Why it lost | See |
|---|---|---|
Infer it from mem::allocations() (a kept thread’s count keeps growing, a new one starts near zero) |
it reads one thing to learn another; a test that says “same thread” should say it | D1 |
thread::id() -> int, as Java’s Thread.threadId() and Python’s threading.get_ident() |
a number invites arithmetic and ordering that mean nothing, and pins the representation (owner) | D1 |
The operating system’s thread id or handle inside, as C++’s std::thread::id wraps pthread_t |
the system hands a value out again after its thread ended, so a new thread can look like the kept one, which is the mistake the test is to catch | D2 |
| The address of a per-thread variable inside | costs nothing, but is reused after a thread ends, the same flaw | D2 |
| No thread name at all, as Go refuses a goroutine id | Go refuses it to keep programs from goroutine-local state; Nikaia has no thread-local state to key, and the id names an OS thread, not a task | D3 |
thread::id() gives an opaque thread::Id.std::thread::id() -> thread::Id names the thread that calls it, as Rust’s
ThreadId and C++’s std::thread::id do. A thread::Id can be compared with
== and != and used as a key of a map or a member of a set (it has a hash);
it has no arithmetic, no order and no conversion to a number. Two calls on the
same thread give equal ids; two different threads never do.
let first = thread::id()
// ...
assert thread::id() == first
ADR-400 refused a type that allows only == for an address. A thread::Id
is not an address and is more than ==: it is an identifier, usable as a
key. That refusal holds for addresses, not for identifiers.
The id is taken from a process-wide counter the first time a thread calls
thread::id(), and kept for the thread’s life. Ids are never handed out
twice, so a thread started after another one ended never looks like it.
A thread that never asks costs nothing, so starting many threads stays as
cheap as before. The id is not the operating system’s thread id and not an
address. Because the type is opaque, this inside may change without breaking
a program.
std::thread holds nothing else.A task may be resumed on another thread than the one it began on; id()
names the thread at the moment of the call. Concurrency stays with tasks
(Part II); std::thread gains no way to start, join or name a thread.
std::thread with the type Id and the
function id().== and hash, the function, tests.Decided November 18, 2026 (owner).