CS3413/Lab3/examples/4-passing_data_wrongly_fixed.c

31 lines
734 B
C
Raw Normal View History

2023-10-04 18:07:09 -03:00
#include<pthread.h>
#include<stdio.h>
#include <stdlib.h>
typedef struct thread_args{
char letter;
int id;
}Args;
void* run (void* arg){
Args* argg = (Args*) arg;
int i = argg->id;
printf("Hello from run %d\n", i);
return NULL;
}
int main(int argc, char** argv){
pthread_t thread[10];
int i = 0;
for (i = 0 ; i < 10; i++){
Args* argg = (Args*) (malloc(sizeof(Args)));
argg->letter = 'q';
argg->id = i;
pthread_create(&(thread[i]), NULL, &run,(void*)argg);
}
for (i = 0 ; i < 10; i++){
pthread_join(thread[i],NULL);
}
// Free the resources somewhere, for long running program. Or let the OS handle that
printf("In main");
return 0;
}