Advertisement
Guest User

Untitled

a guest
Feb 11th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1. #include "pthread.h"
  2. #include "stdio.h"
  3.  
  4.  
  5. #define N_THREADS 10
  6. #define N_RUNS 10000
  7.  
  8.  
  9. void* readStuff(void* a)
  10. {
  11.   int i;
  12.   for (i = 0; i< N_RUNS; i++){
  13.   ;
  14.   }
  15.   pthread_exit((void*)0);
  16. }
  17.  
  18. void* addRemove(void* a)
  19. {
  20.    int i;
  21.    for (i = 0; i< N_RUNS; i++){
  22.    ;
  23.    }
  24.    pthread_exit((void*)0);
  25. }
  26.  
  27. int main()
  28. {
  29.  
  30. pthread_t threads[N_THREADS];
  31. pthread_attr_t attr;
  32. int i;
  33. int rc;
  34. int status;
  35.  
  36. printf("Testing whether there are problems with concurrency ...");
  37.  
  38. pthread_attr_init(&attr);
  39. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  40.  
  41. for (i = 0; i < N_THREADS; i++){
  42.   if (i) {
  43.     rc = pthread_create(&(threads[i]), &attr, addRemove, 0);
  44.   } else {
  45.     rc = pthread_create(&(threads[i]), &attr, readStuff, 0);
  46.   }
  47.   if (rc) return rc;
  48. }
  49.  
  50. for(i = 0; i < N_THREADS; i++) {
  51.   rc = pthread_join(threads[i], (void*) &status);
  52. //  if(rc == 3)
  53.       printf("rc is %d. i is %d\n", rc, i);
  54. //  if (rc) return rc;
  55.   if (status) return status;
  56.   printf(".");
  57. }
  58.  
  59. pthread_attr_destroy(&attr);
  60.  
  61. return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement