Problem with Posix Streams

I am trying to understand pthreads using an example. I made the following code that gives different answers every time I run! Can someone explain the error please? TIA, Sviiya

The code is here:

#include <pthread.h> #include <stdio.h> #include <stdlib.h> #define NUM_THREADS 4 struct thread_data { int thread_id; int sum; }; struct thread_data thread_data_array[NUM_THREADS]; void *PrintHello(void *threadarg) { struct thread_data *my_data; int taskid; int sum; my_data = (struct thread_data *) threadarg; taskid = my_data->thread_id; sum = my_data->sum; printf("Hello World! It me, thread #%d\n", taskid); return my_data; } int main () { pthread_t threads[NUM_THREADS]; int rc; long t; int sum=0; for (t=0; t < NUM_THREADS; t++) { printf("Hi! %ld\n", t); threads[t] = t; thread_data_array[t].thread_id = t; thread_data_array[t].sum = sum; rc = pthread_create(&threads[t], NULL, PrintHello, (void *) &thread_data_array[t]); } return 0; } 

The conclusion is here:

 [ user@office tmp]$ ./a.out Hi! 0 Hi! 1 Hello World! It me, thread #0 Hello World! It me, thread #1 Hi! 2 Hi! 3 Hello World! It me, thread #2 [ user@office tmp]$ ./a.out Hi! 0 Hi! 1 Hello World! It me, thread #0 Hello World! It me, thread #1 Hi! 2 Hi! 3 Hello World! It me, thread #2 Hello World! It me, thread #3 [ user@office tmp]$ ./a.out Hi! 0 Hello World! It me, thread #0 Hi! 1 Hello World! It me, thread #1 Hi! 2 Hello World! It me, thread #2 Hi! 3 Hello World! It me, thread #3 [ user@office tmp]$ ./a.out Hi! 0 Hi! 1 Hello World! It me, thread #0 Hi! 2 Hi! 3 Hello World! It me, thread #3 [ user@office tmp]$ 
0
source share
6 answers

No errors, this is exactly how threads work. Your main thread creates new threads that are "ready to run" at this point. At some point in time (determined by the OS), your main thread will be suspended, and one of the other threads will work for a certain period of time (usually several tens of milliseconds). The system will continue switching between threads as long as there are current threads for your program.

The exact point in time when your main thread will be interrupted, and one of the other threads can print it. Hello World will depend on what the OS scheduler decides, depending on how long your main thread has been running, other activity on the system, external (I / O) events, etc.

EDIT to include my comment below:

The reason you see 3, then 4, and then only 2 "Hello Worlds" is because you created the threads, but you didn’t wait until they really started planning and starting. When your main loop ends, the program is dead, regardless of whether your other threads had the ability to run. If you want all your threads to be finished, you need to do pthread_join for each of your threads before returning from the main one.

+4
source

If you mean the order of the answers, yes, that will be different, since which thread is working is decided by the Linux scheduler.

To develop: as soon as you create your threads, the order in which they receive processor time depends on the main OS scheduler (here, the Linux scheduler). It may not be every time every time.

+1
source

The reason you don't see other threads executing in accordance with your comments on other answers is because your main() function (and therefore your program) returns before other threads were able to execute. If you put the sleep(5) symbol before returning main() , you will see that they are executed.

+1
source

I understand that this message is outdated, but as an alternative, you can use pthread_exit() at the end of main() so that main() will wait for all its peer threads to complete before exiting prematurely.

+1
source

I agree with two other answers. It may also be worth noting that the process exits immediately after creating the threads. He will create all 4, but each may not have enough time to do anything before the program is released.

0
source

Well, finally, I added the extracode, as suggested by the experts, and now it works fine. Thanks again. :)

 for ( t = 0; t < NUM_THREADS; t++ ) { (void) pthread_join(threads[t], NULL); } 
0
source

All Articles