Bug 15076 – Get ID of current thread

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
Windows
Creation time
2015-09-17T01:05:00Z
Last change time
2015-10-04T18:21:08Z
Keywords
pull
Assigned to
nobody
Creator
dlang-bugzilla

Comments

Comment #0 by dlang-bugzilla — 2015-09-17T01:05:17Z
core.thread is missing a function to get the ID of the current thread. getpid is insufficient, as even on POSIX it will return the same value for all threads: ///////// test.d ///////// import core.thread; import std.stdio; void main() { writeln(getpid()); new Thread({ writeln(getpid()); }).start(); } ////////////////////////// Essentially, it should be GetCurrentThreadId() on Windows and pthread_self() on POSIX (same logic as for initializing Thread.m_addr).
Comment #1 by dlang-bugzilla — 2015-09-17T01:09:37Z
Alternatively this could go into std.process, alongside thisProcessID.
Comment #2 by issues.dlang — 2015-09-17T09:49:08Z
I would think that the most natural thing to do (and what most folks would likely look for) would be a static function/property on core.thread.Thread - e.g. Thread.currThreadID - since that's where the thread stuff is.
Comment #3 by schveiguy — 2015-09-17T15:07:56Z
What is the use case for this? Why doesn't Thread.getThis suffice?
Comment #4 by dlang-bugzilla — 2015-09-17T15:51:45Z
https://github.com/D-Programming-Language/druntime/pull/1388 https://github.com/D-Programming-Language/phobos/pull/3654 (In reply to Jonathan M Davis from comment #2) > I would think that the most natural thing to do (and what most folks would > likely look for) would be a static function/property on core.thread.Thread - > e.g. Thread.currThreadID - since that's where the thread stuff is. Then we would have three conventions for such functions. Currently, there is core.thread.getpid and std.process.thisProcessID. Introducing something that's unlike neither of these would be very inconsistent. (In reply to Steven Schveighoffer from comment #3) > What is the use case for this? Why doesn't Thread.getThis suffice? Thread.getThis: 1. Returns a pointer, not the OS identifier. The pointer cannot be unique per-process (esp. in the case of multiple D runtimes). 2. Requires that the current thread is registered with the D runtime. If it isn't, it returns null.
Comment #5 by github-bugzilla — 2015-09-27T12:38:18Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/1699b5fc466232087d876bd41e214ec41b290e1c fix Issue 15076 - Get ID of current thread https://github.com/D-Programming-Language/phobos/commit/d6fa6833fa45fa471ab14f0ca2bab1f7c9ba4006 Merge pull request #3654 from CyberShadow/pull-20150917-013849 fix Issue 15076 - Get ID of current thread
Comment #6 by github-bugzilla — 2015-10-04T18:21:08Z