You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Being able to get the current thread ID would be handy for various things. Here are two specific examples that are relevant to the project I'm working on at the moment:
When implementing a mutex, you can store the ID of the locking thread to help detect deadlock where the same thread tries to lock it again, or to detect a different thread trying to unlock.
When implementing a thread-safe allocator, you can map different threads to different subheaps to help avoid false sharing (note that a thread-local variable is unhelpful here, because a thread may sometimes need to access the subheap of another thread).
Strawman proposal for code in std/os/index.zig:
constThreadId=usize; // or something else, possibly depending on builtin.ospubconstThread=struct {
// ...existing code.../// Returns the ID of this thread object.pubfnid(self: *constthis) ThreadId {
returnif (use_pthreads) {
self.data.handle
} elseswitch (builtin.os) {
builtin.Os.linux=>@bitCast(u32, self.data.pid),
builtin.Os.windows=>@ptrToInt(self.data.handle),
else=>@compileError("Unsupported OS"),
};
}
/// Returns the ID of the calling thread.pubfncurrent() ThreadId {
returnif (use_pthreads) {
c.pthread_self()
} elseswitch (builtin.os) {
// ...etc... (if I knew how to do this part, I would've just sent a PR)
};
}
};
The text was updated successfully, but these errors were encountered:
Being able to get the current thread ID would be handy for various things. Here are two specific examples that are relevant to the project I'm working on at the moment:
Strawman proposal for code in
std/os/index.zig
:The text was updated successfully, but these errors were encountered: