If I synchronize the threads with join() , given the order of the calls to join, why do I sometimes see the output of t1 after t2 ?
i.e.
#include <thread> void callFromThread(int id) { int i = 1000; while(i != 0) { printf("%s %d\n", "hi from thread", id); i--; } } int main(void) { std::thread t1 (callFromThread, 1); std::thread t2 (callFromThread, 2); t1.join(); t2.join(); printf("%s\n", "bye from main!"); return 0; }
I could understand the behavior if I had some interleaving at the beginning before the connection calls, followed by all the remaining t1 outputs, followed by the remaining t2 outputs. But instead, I see all t2, then all t1, or vice versa.
c ++ multithreading
wulfgarpro
source share