Advertisement
minh_tran_782

cond_usg.c

Feb 28th, 2022
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  #include <assert.h>
  4.  #include <pthread.h>
  5.  
  6.  #define NUM_THREADS 3
  7.  #define TCOUNT 100
  8.  #define COUNT_LIMIT 20
  9.  
  10.  int count = 10;
  11.  pthread_mutex_t count_mutex ;
  12.  pthread_cond_t count_threshold_cv ;
  13.  
  14.  void *inc_count (void * tid ){
  15.  int i ;
  16.  long my_id = (long) tid ;
  17.  for ( i = 0 ; i < TCOUNT; i++){
  18.  pthread_mutex_lock(&count_mutex ) ;
  19.  count++;
  20.  if ( count == COUNT_LIMIT){
  21. printf( "inc_count ( ) : thread %ld , count = %d ,threshold reached . \n" , my_id, count ) ;
  22.  pthread_cond_signal(&count_threshold_cv ) ;
  23.  printf( "Just sent s i g n a l \n" ) ;
  24.  }
  25.  
  26.  printf( "inc_count ( ) : thread %ld , count = %d ,unlocking mutex\n" , my_id, count ) ;
  27.  pthread_mutex_unlock(&count_mutex ) ;
  28.  sleep( 1 ) ;
  29.  }
  30.  pthread_exit (NULL) ;
  31.  }
  32.  
  33.  void *watch_count(void * tid ){
  34.  long my_id = (long) tid ;
  35.  printf( " Starting watch_count ( ) : thread %ld \n" , my_id ) ;
  36.  
  37.  pthread_mutex_lock(&count_mutex ) ;
  38.  while( count < COUNT_LIMIT){
  39. printf( "watch_count ( ) : thread %ld , count = %d ,waiting . . . \n" , my_id, count ) ;
  40.  pthread_cond_wait(&count_threshold_cv ,&count_mutex ) ;
  41.  printf( "watch_count ( ) : thread %ld . Condition s i g n a l received .Count = %d\n" ,my_id, count) ;
  42.  printf( "watch_count ( ) : thread %ld Updating the count value . . . \n" , my_id ) ;
  43.  count += 80;
  44. printf( "watch_count ( ) : thread %ld count now = %d\n" , my_id, count ) ;
  45.  }
  46.  printf( "watch_count ( ) : thread %ld . Unlocking mutex . \n" , my_id ) ;
  47.  pthread_mutex_unlock(&count_mutex ) ;
  48.  pthread_exit (NULL) ;
  49.  }
  50.  
  51.  int main( int argc , char** argv )
  52.  {
  53.  int i , rc ;
  54.  pthread_t p1 , p2 , p3 ;
  55.  long t1 = 1 , t2 = 2 , t3 = 3 ;
  56.  pthread_attr_t attr ;
  57.  
  58.  printf( "main : begin\n" ) ;
  59.  pthread_mutex_init(&count_mutex , NULL) ;
  60.  pthread_cond_init(&count_threshold_cv , NULL) ;
  61.  
  62.  pthread_attr_init(&attr) ;
  63.  pthread_attr_setdetachstate(&attr , PTHREAD_CREATE_JOINABLE) ;
  64.  
  65.  pthread_create(&p1 , &attr , watch_count , (void*)t1 ) ;
  66.  pthread_create(&p2 , &attr , inc_count , (void*) t2 ) ;
  67.  pthread_create(&p3 , &attr , inc_count , (void*) t3 ) ;
  68.  
  69.  rc = pthread_join (p1 , NULL) ; assert ( rc == 0 ) ;
  70.  rc = pthread_join (p2 , NULL) ; assert ( rc == 0 ) ;
  71.  rc = pthread_join (p3 , NULL) ; assert ( rc == 0 ) ;
  72.  
  73.  printf( "main : finish,final count = %d\n" , count ) ;
  74.  
  75.  pthread_attr_destroy(&attr ) ;
  76.  pthread_mutex_destroy(&count_mutex ) ;
  77.  pthread_cond_destroy(&count_threshold_cv ) ;
  78.  pthread_exit (NULL) ;
  79.  
  80.  return 0 ;
  81.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement