I want to create multiple threads (10 in the example below) and have each of them run a function.
Here is my code:
#include <stdio.h>
#include <pthread.h>
typedef struct arg_struct {
int id;
int val;
} arg_struct;
void *printarg(void *params) {
arg_struct *args = (arg_struct *) params;
printf("id %i value %i\n", args->id, args->val);
return 0;
}
int main() {
int i = 0;
pthread_t threads[10];
for (i = 0; i < 10; i++) {
arg_struct arg;
arg.id = i;
arg.val = i + 10;
pthread_create(&(threads[i]), NULL, &printarg, &arg);
}
for (i = 0; i < 10; i++) {
pthread_join(threads[i], NULL);
}
return 0;
}
I was expecting an output like this during execution:
id 0 value 10
id 1 value 11
id 2 value 12
id 3 value 13
id 4 value 14
id 5 value 15
id 6 value 16
id 7 value 17
id 8 value 18
id 9 value 19
But instead I get outputs like this:
id 2 value 12
id 3 value 13
id 3 value 13
id 4 value 14
id 6 value 16
id 6 value 16
id 9 value 19
id 9 value 19
id 9 value 19
id 9 value 19
Every time I rerun my program the output changes slightly. The id's & value's repeat and the pattern seems irregular.
What is happening? Are the repeats happening because the child threads are also creating threads in the for loop? If so, I am confused because in many of the examples of pthread_create I read, people seemed to be using a loop which iterates N times to create N threads (exactly like above). Therefore I assumed that the above code will have 1 master thread create N child threads? Am I wrong? Thanks in advance.