XAgent-Smith

Asynchronous_3_thread_with_combination_of_mutex_and conditi_

Nov 24th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <pthread.h>
  4. #include <unistd.h>
  5. #include <sched.h>
  6. #include <signal.h>
  7. #include <stdlib.h>
  8. /*Asynchronous 3 threads through mutex and condition variable simple count example for sake  of clearity it just incriment count and decrement */
  9. /*some unused variable */
  10.  
  11. #define HIGHYPRIORTY 10
  12.  
  13. static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER ;
  14. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER ;
  15.  
  16.  
  17. static int count = 0 ;
  18. int done = 0 ;
  19. void *incremant(void *arg)
  20. {
  21. //int error = 0 ;
  22. pthread_mutex_lock(&lock);
  23.  
  24. count+=100;
  25.  
  26.  pthread_mutex_unlock(&lock) ;
  27.  ++done ;
  28. pthread_cond_signal(&cond) ;
  29. printf("\n00001\n");
  30. }
  31. void *decremeant(void *arg )
  32. {
  33.  pthread_mutex_lock(&lock) ;
  34. count-- ;
  35.  
  36. pthread_mutex_unlock(&lock);
  37. ++done ;
  38. pthread_cond_signal(&cond) ;
  39. printf("\n00002\n");
  40. }
  41.  
  42. void *getcount(void *arg)
  43. {
  44. int error ;
  45.  
  46. if((error = pthread_mutex_lock(&lock)))
  47. pthread_exit(NULL);
  48.  
  49.  
  50. while(done!=2)
  51. pthread_cond_wait(&cond ,&lock) ;
  52.  
  53.  
  54.  printf("\n00003\n");
  55.  
  56. pthread_mutex_unlock(&lock) ;
  57. return (void*)count;
  58.  
  59. }
  60.  
  61. void *(*routines[])(void*)={incremant,decremeant,getcount};
  62.  
  63.  
  64. pthread_attr_t *make_high_priority_attr(int priority)
  65. {
  66. pthread_attr_t *attr ;
  67.  
  68. int error ;
  69. struct sched_param parm ;
  70.  
  71. if((attr = (pthread_attr_t *)malloc(sizeof(pthread_attr_t)))== NULL)
  72. {      
  73.     return NULL ;
  74. }
  75.  
  76. if(!(error = pthread_attr_init(attr))&&!(error = pthread_attr_getschedparam(attr , &parm)))
  77. {
  78.     parm.sched_priority = priority ;
  79.      error = pthread_attr_setschedparam( attr , &parm ) ;
  80. }
  81.  
  82. if(error)
  83. {
  84.      free(attr)     ;
  85.        errno = error  ;
  86.      return NULL    ;
  87. }
  88.  
  89. return attr ;
  90.  
  91. }
  92.  
  93.  
  94. int main()
  95. {
  96.  
  97.  
  98. pthread_attr_t *attr   ;
  99.  
  100. pthread_t thread[3]    ;
  101.  
  102. sigset_t  mask ;
  103. sigset_t oldmask ;
  104. void *statuscount = NULL ;
  105.  
  106. if((sigfillset(&mask)) == -1 )
  107. {
  108. perror("Warrning  :unable to fill mask set") ;
  109. }
  110.  
  111. if((sigprocmask(SIG_SETMASK, &mask, &oldmask))==-1)
  112. {
  113.     perror("Unable to block all signals");
  114.      return(-1)     ;
  115. }
  116.  
  117. if((attr=make_high_priority_attr(HIGHYPRIORTY))==NULL)
  118. {
  119.   perror(strerror(errno)) ;
  120.   return(-1) ;
  121. }
  122.  
  123. if(pthread_attr_setscope(attr , PTHREAD_SCOPE_SYSTEM))
  124. {
  125.    perror("Warrning: Unable Get system scope");
  126.    
  127. }
  128. if(pthread_attr_setdetachstate(attr , PTHREAD_CREATE_JOINABLE))
  129. {
  130.  
  131. perror("Unable to set detach state");
  132.  return -1 ;
  133.  
  134. }
  135. for(int i = 0; i < 3 ; i++)
  136. {
  137.  
  138. if(pthread_create(&thread[i], attr ,(void *(*)(void*))*(routines+i),NULL))
  139. {
  140.     fprintf(stderr,"Unable to creat thread%d",i) ;
  141.      free(attr)  ;
  142.      return(-1) ;
  143. }
  144. printf("%d",i);
  145. }
  146.  
  147. if((sigprocmask(SIG_SETMASK,&oldmask,NULL)) == -1)
  148. {
  149.     perror("Warrning:Unable to reset Signal state")  ;
  150. }
  151.  
  152. for(int i = 0; i < 3 ;i++)
  153. {
  154.   if(pthread_join(thread[i],&statuscount))
  155.   {
  156.     fprintf(stderr ,"unable to join thread%d",i) ;
  157.      free(attr)    ;
  158.      return(-1)    ;
  159.   }
  160. }
  161.  
  162. printf("\n\nCount=%d",((int)statuscount)) ;
  163.  
  164. free(attr)  ;
  165.  
  166. return 0 ;
  167.  
  168. }
Add Comment
Please, Sign In to add comment