Reputation: 6924
If I'm synchronizing threads with join()
, considering 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 make sense of the behaviour if I had some interleaving in the beginning before the join calls, followed by all remaining t1 outputs, followed by the remaining t2 outputs. But, instead I'm seeing all t2 then all t1, or vice-versa.
Upvotes: 7
Views: 2467
Reputation: 26476
join
affects the current thread you call join
, not the thread which is joined.
basically, join makes the current thread wait for another thread to finish execution. it has no effect on when the other thread is scheduled to run, or on what order compared to other threads.
in your example, there is no guarantee which of the thread t1
and t2
will run and finish first. the only guarantee is that the main thread waits for t1
first, then for t2
, then logs a message to stdout.
Upvotes: 11
Reputation: 121347
The order in which you join doesn't determine or influence the order in which the threads are executed. So, the output from both threads could be in any order (or interleaved).
Upvotes: 2
Reputation: 76235
join
doesn't have any effect on the thread that it's applied to. It just blocks the thread that called it until the thread that it's applied to finishes, and then continues execution. So the order of calls to join
doesn't do anything to the order in which the threads are run.
Incidentally, in
std::thread t1(callFromthread, 1);
if (t1.joinable()) t1.join();
the test is redundant. Unless you call detach
, std::thread
objects are joinable.
Upvotes: 1
Reputation: 743
The order of the calls doesn't mean that your output will be ordered the same way, and since you're executing the threads at "the same time" you can't control wich intruction is executed first in the CPU.
If you need to force that t1 make something before t2 just use Semaphores.
Upvotes: 1