Reputation: 8930
I did the following experiment:
#include <iostream>
#include <thread>
void t()
{
std :: cout << std :: this_thread :: get_id() << std :: endl;
}
void f()
{
std :: cout << std :: this_thread :: get_id() << std :: endl;
}
int main()
{
std :: cout << std :: this_thread :: get_id() << std :: endl;
for(unsigned int i = 0; i < 1000; i++)
{
std :: thread d(t);
d.join();
std :: thread g(f);
g.join();
}
}
and noticed that the resulting list of thread_id
s would always be the same. In other words, in this case the thread_id
was continuously re-assigned whenever the thread was joined and re-opened.
Now, can I have any form of certainty that this will always happen? Or can it happen that a thread_id
is assigned only once, then always different thread_id
s get assigned in a random-like fashion?
Upvotes: 0
Views: 223
Reputation: 69854
There's some documentation here: http://en.cppreference.com/w/cpp/thread/thread/id
emphasis is mine
The class thread::id is a lightweight, trivially copyable class that serves as a unique identifier of std::thread objects.
Instances of this class may also hold the special distinct value that does not represent any thread. Once a thread has finished, the value of std::thread::id may be reused by another thread.
This class is designed for use as key in associative containers, both ordered and unordered.
Upvotes: 0
Reputation: 11002
No, you cannot assume that it will always be the same. On VS2015 I get the following:
...
2444
18472
29912
25180
6612
29440
13220
4684
14004
12388
16424
26320
25948
28076
30904
6396
1160
4228
...
Upvotes: 3