You are viewing a single comment's thread from:
RE: Networking - C Threads in Linux
Thanks @drifter1. You made me easy to understand my Real-time OS subject. But I want to clarify last example coding. Is it all threads sequentially run after another? And what happens at rand()?
It's "Random" how threads will run on the CPU.
To make it even easier to see that this exact thing happens I put a random sleep timer for each thread to be run so that they don't quit directly...
Of course thread 0 will be "made" much faster then thread N-1, but which thread will start running the example() code is random, cause the "timing" of the CPU is not so sequential at all.
Even if you have an 4Core/8Thread i7 (like me) you will see that 8 threads will not run in this exact order or even at the same time, but thread 7 might even print out a message before thread 0...
Thanks @drifter1 for detailed explanation. Could you please tell me what happens below. I think I confused with thread joining.
for(i = 0; i < N; i++){
pthread_join(threads[i], NULL);
}
Haha of course...
When a process or thread runs the main process/thread that created it must finish after it.
For threads we use the function pthread_join() with an important parameter being the thread_id (the number which represents the thread in the Operating system).
So, because we have N threads and because the id's are stored in an array, we have a for loop so that the main process/thread waits for all of them :)