2

I am just new to threads in C. So starting with the very basics. I am just trying to create threads and using mutual exclusion on them. I have declared three function and creating 3 threads for them but every time I execute my program not all three processes get executed all the time

Please give a solution to this.

Abhishek Gangwar
  • 1,697
  • 3
  • 17
  • 29

1 Answers1

2

You have to pass the thread object, in pthread_join(), See updated line#35,38,41.

The definition of the function is int pthread_join(pthread_t thread, void **retval);

This shall help.

 34     thread1=pthread_create(&trd1,NULL,process1,(void *)nargs1);
 35     pthread_join(trd1, NULL);
 36
 37     thread2=pthread_create(&trd2,NULL,process2,(void *)nargs2);
 38     pthread_join(trd2, NULL);
 39
 40     thread3=pthread_create(&trd3,NULL,process3,(void *)nargs3);
 41     pthread_join(trd3, NULL);
Pawan
  • 1,537
  • 1
  • 15
  • 19
  • Still not getting the desired output. Although if i put sleep before the return of the main function It works. Can anybody suggest why above code is not working as join does the same thing of waiting. – Abhishek Gangwar Aug 15 '15 at 09:02
  • Thanks for the answer it worked.Was joining the threads with wrong parameters. – Abhishek Gangwar Aug 15 '15 at 09:41