Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6.  
  7. pthread_t tid[3];
  8.  
  9. // method for the threads to run
  10. void * test(){
  11. pthread_t id = pthread_self();
  12. int threadNum = 0;
  13. if(pthread_equal(id,tid[0]))
  14. threadNum = 1;
  15. else if(pthread_equal(id, tid[1]))
  16. threadNum = 2;
  17. else if (pthread_equal(id, tid[2]))
  18. threadNum = 3;
  19. int i;
  20. for(i = 1; i < 11; i ++)
  21. printf("%d: %d\n",threadNum,i);
  22. }
  23.  
  24. // main method creates threads
  25. int main(void){
  26. int i = 2;
  27. int err;
  28. while(i>=0){
  29. if(pthread_create(&(tid[i]),NULL,&test,NULL) != 0) printf("Error in creating thread");
  30. i--;
  31. }
  32. //sleep(1);
  33. wait();
  34. printf("Application is finished\n");
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement