c - Getting wrong value for ID in pthreads -


i'm trying output be

starting professor 1 starting professor 2 starting professor 3 ... 

but never "starting professor 1" when num_professors = 2. thought making array of ids save whole passing in of address of id, apparently not. there's 70 other things have project , having roadblock on simple thing (that takes few seconds fix) quite frustrating least. appreciated

void * professorfunc(void *p){     sem_wait(&workassignment);     if(buffer == 0){             buffer++;             professor *professor = (professor*)p;             fprintf(stdout,"starting professor %d\n", *professor->id);     }     buffer = 0;     sem_post(&workassignment);     pthread_exit(0); } int main(int argc, char ** argv){ //semaphore intialization     buffer = 0;     if(sem_init(&workassignment, 0, 1)){             printf("could not initialize semaphore.\n");             exit(1);     }      //creating threads     pthread_t professor[num_professors];     professor *p;     int ids[num_professors];     int i;     p = malloc (sizeof (*p) * num_professors);      for(i = 0; < num_professors; ++i){             ids[i] = + 1;             p->id = &ids[i];             //printf("id: %d\n", *p->id);             if(pthread_create(&professor[i], null, professorfunc, p) != 0){                     perror("pthread_create");                     exit(1);             }             //printf("yo i'm here after function now\n");     }     for(i = 0; < num_professors; ++i){             if(pthread_join(professor[i], null) != 0){                     perror("pthread_join");                     exit(1);             }     }     free(p); } 

this line:

if(pthread_create(&professor[i], null, professorfunc, p) != 0){ 

should be:

if(pthread_create(&professor[i], null, professorfunc, &p[i]) != 0){ 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -