+ 7

Why cout << this_thread::get_id(); give me 1 on code::block and when on visual studio it give me like 8348

Do 8348 with 1 have different meaning? or else? Code: cout << "started thread with id = " << this_thread::get_id() << endl;

1st Jan 2019, 4:19 AM
ZΛRTHΛИ
ZΛRTHΛИ - avatar
5 Respuestas
+ 6
Zarthan No, what I meant was 1 must have probably been the id of a thread that must have been initiated by some program during startup itself, and it was a coincidence that your program was allocated with the same id. Usually get_id returns a long number that is somehow formulated as an integer when we use << on it, but it is not directly castable to int ( you can't do int id = this_thread::get_id(); ). There is a function joinable on the other hand that return a bool value (1/0) to denote if the thread is joinable or not, respectively.
1st Jan 2019, 1:08 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 5
http://www.cplusplus.com/reference/thread/thread/id/ As per this page, ids of an unjoinable thread may be reused in a new thread: "Note that certain library implementations may reutilize the thread::id value of a terminated thread that can no longer be joined." But the behaviour, in the end, is undefined, and probably coincidental.
1st Jan 2019, 6:09 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 5
Kinshuk Vasisht you mean 1 is stand for joinable and 0 unjoinable?
1st Jan 2019, 7:44 AM
ZΛRTHΛИ
ZΛRTHΛИ - avatar
+ 3
Hmmm.. You probably right Kinshuk Vasisht, i've make 3 thread and have the id 1,2,3 and it seems every new thread that created on my code::block always increase 1. I think those id that was initiated by some program was used to difference every thread. P.S please correct me if there is something wrong, thanks
1st Jan 2019, 9:04 PM
ZΛRTHΛИ
ZΛRTHΛИ - avatar
+ 3
Zarthan Yes, that is why the id's differ.
2nd Jan 2019, 1:51 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar