Reputation: 1601
I am writing a multi-thread program, where one thread executes a lot of system calls (like read, write), and other thread executes normal calls like printf. Suppose thread A is for normal calls, and thread B is for system calls, my main function is like
int main()
{
pthread_t thread_A;
pthread_t thread_B;
pthread_create(&thread_B,NULL,&system_call_func,NULL);
pthread_create(&thread_A,NULL,&printf_func,NULL);
pthread_join(thread_B,NULL);
pthread_join(thread_A,NULL);
printf("Last thread to be executed was %c\n",write_last);
return 0;
}
By this, I found that the thread with system calls is executed last always. Even if I change the order of thread creation and joining, it is still thread B. I have two questions, does the order of thread creation/joining matters? and is it because of the system calls that thread B is always executing last?
Upvotes: 2
Views: 617
Reputation: 215287
You're just measuring which thread finishes first, not which one runs first. Assuming they both run in parallel and start at roughly the same time, the one that spends less time working is going to finish first.
If you want to observe the sequence of operations in both, run the program under strace -f
, but be aware that the overhead of tracing slows things down a lot and tends to eliminate parallelism in the traced program except when it's doing purely computational tasks with no system calls.
Upvotes: 2