10

From my experience, it seems that the result of

std::this_thread::get_id()

is unique across process: ids are different from one process to another.

Is this guaranteed by the standard?

Benoit Blanchon
  • 13,364
  • 4
  • 73
  • 81
  • Are you asking specifically for gcc on linux right now or for C++ in general? The answers seem to differ. – nwp Sep 28 '15 at 14:01
  • My question is about the C++ standard in general. I'm looking for an answer on all platforms and all compilers. – Benoit Blanchon Sep 28 '15 at 14:02
  • 5
    The standard doesn't have any concept of a "process", any notion that several programs may run simultaneously and interact. So the question cannot even be formulated in the standard terms, let alone answered. All you can ask is "what would a typical implementation do?" – Igor Tandetnik Sep 28 '15 at 14:57

2 Answers2

5

std::thread is implemented on top of pthreads in an environment supporting pthreads. So its becomes there is no (portable) guarantee.

From pthread_self manual:

Thread IDs are guaranteed to be unique only within a process. A
thread ID may be reused after a terminated thread has been joined, or a detached thread has terminated.

Galimov Albert
  • 7,269
  • 1
  • 24
  • 50
  • 3
    This doesn't answer the question of what the C++ standard requires, it just means that if the C++ standard requires thread IDs to be unique across processes, an implementation cannot simply re-use the pthread IDs. (But it does make it likely that the C++ standard doesn't require thread IDs to be unique across processes.) –  Sep 28 '15 at 13:44
  • @hvd yes, but std::thread is implemented on top of pthreads in an environment supporting pthreads. So its becomes there is no (portable) guarantee. – Galimov Albert Sep 28 '15 at 13:47
  • 2
    Can you add a source for the clam that `std::thread` is implemented of top of pthreads where available? I cannot imagine the standard requiring this. I'm also fairly sure that Windows is a counterexample, because pthreads are available and the are not used to implement `std::thread`, they both use native WinApi implementations instead. But then again it would not be the first time that Microsoft is not standard compliant. – nwp Sep 28 '15 at 13:56
  • @PSIAlt, you're right [std::thread::id is the same as pthread's id](http://ideone.com/YBjI8s) – Benoit Blanchon Sep 28 '15 at 13:59
  • So the final answer is: *No, uniqueness is not guaranteed across processes*. – Benoit Blanchon Sep 28 '15 at 14:01
  • 1
    @PSIAlt It might be the implementations built on top of pthreads are not standard compliant. – nos Sep 28 '15 at 14:02
  • 1
    @nos i think its not very important what standard says, since this guarantee is not really preserved – Galimov Albert Sep 28 '15 at 14:10
  • 4
    Er... For a question that specifically asks "Is this guaranteed by the standard?", how could you possibly comment "i think its not very important what standard says"? It's a poor answer unless the OP doesn't actually care what the standard says (as the acceptance of your answer would suggest), in which case it's a poor question for asking something other than what the OP is actually after. –  Sep 28 '15 at 14:29
  • Since there appears to be a desire to be exceedingly pedantic, the C++ standard doesn't acknowledge the existence of other processes, thus it makes no statement about the uniqueness of a `thread::id` with these mythical "other processes". It does acknowledge the existence of multiple threads. It does state that a `thread::id` is unique between each thread of execution, and that it may be reused from a previously joined thread. It is also noted that C++ Threads are intended to map one-to-one to operating system threads. – Andre Kostur Sep 29 '15 at 00:55
2

The standard grantees that thread ids are unique across different threads, it also says that terminated thread ids might be reused. It doesn't specify processes, and doesn't acknowledged their existence, so, therefore, it doesn't guarantee uniqueness across processes.

30.3.1.1

  1. An object of type thread::id provides a unique identifier for each thread of execution and a single distinct value for all thread objects that do not represent a thread of execution (30.3.1). Each thread of execution has an associated thread::id object that is not equal to the thread::id object of any other thread of execution and that is not equal to the thread::id object of any std::thread object that does not represent threads of execution.
  2. thread::id shall be a trivially copyable class (Clause 9). The library may reuse the value of a thread::id of a terminated thread that can no longer be joined.

The standard also hides away the implementation of a thread::id, it could be a int or something else.