I am new to multi threading and am following “Advanced programming in unix environment”. I am not able to get the order in which the threads are executed. I have the following code.
int err1 = pthread_create(&first, NULL, disp, a);
int err2 = pthread_create(&second, NULL, disp, b);
int err3 = pthread_create(&third, NULL, disp, c);
But the thread related to third tid is executing first, then the second and finally the first. Not sure if this is the behavior or something going wrong.
Thx!
Rahul.
There is no guarantee about the order of executing the code once they are created.
Only thing that can be guaranteed is that Thread 3 will be created after Thread 2 and Thread 2 will be created after Thread 1.
You cannot predict or assume that thread 2 will be spawned only after certain code has executed in thread 1. If you want to achieve something like that you need to provide your some Thread synchronization.