Advertisement
sutozhj

thread test

Nov 29th, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1.  
  2.  
  3. #include <stdio.h>
  4. #include <threads.h>
  5.  
  6.  
  7. int count;
  8. mtx_t gMutex;
  9. int fnC1(void *data){
  10. int i;
  11. for(i=0;i<5;i++){
  12. mtx_lock(&gMutex);
  13.  
  14. count=i;
  15. printf("thread %d : = %d\n", (int)data, count);
  16. mtx_unlock(&gMutex);
  17.  
  18. } return 0;
  19. }
  20.  
  21. int fnC2(void *data){
  22. int i;
  23. for(i=0;i<5;i++){
  24. mtx_lock(&gMutex);
  25. count++;
  26. printf("thread %d : = %d\n", (int)data, count);
  27. mtx_unlock(&gMutex);
  28.  
  29. } return 0;
  30. }
  31.  
  32. int main(void){
  33. // mtx_init(&gMutex, mtx_plain);
  34. int one=1, two=2;
  35. thrd_t thr1, thr2;
  36. /* Create two threads */
  37. if( thrd_create( &thr1, fnC1, (void *)one) != thrd_success)
  38. printf("Thread creation failed: \n");
  39. if( thrd_create( &thr2, fnC2, (void *)two) != thrd_success)
  40. printf("Thread creation failed: \n");
  41. /* Wait for both threads to finish */
  42. thrd_join( thr1, NULL);
  43. thrd_join( thr2, NULL);
  44. printf ("\n");
  45. // mtx_destroy(&gMutex);
  46. return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement