I want to run 4 different threads calling the same method, and I want to make sure that every single run comes from a different running thread.
With the code provided bellow, the method function is ran the expected number of times but it is always done by the same thread (the printed value does not change).
What should I change in the code to assure this condition? (which will result in this example in having 4 different values printed)
EDIT: same code but including an structure to see how the solution wil
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
struct object{
int id;
};
void * function(void * data){
printf("Im thread number %i\n", data->id);
pthread_exit(NULL);
}
int main(int argc, char ** argv){
int i;
int error;
int status;
int number_threads = 4;
pthread_t thread[number_threads];
struct object info;
for (i = 0; i < number_threads; ++i){
info.id = i;
error = pthread_create(&thread[i], NULL, function, &info);
if(error){return (-1);}
}
for(i = 0; i < number_threads; i++) {
error = pthread_join(thread[i], (void **)&status);
if(error){return (-1);}
}
}