【Linux 編程】pthead_cond_t 的使用

來源:互聯網
上載者:User
pthead_cond_t 的使用 原始碼1
 1 #include <stdio.h> 2  #include <pthread.h> 3  #include <stdlib.h> 4  #include <unistd.h> 5   6  pthread_cond_t qready = PTHREAD_COND_INITIALIZER; 7  pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER; 8  int constant = 0; 9  10  void *prints(void *arg)11  {12      pthread_mutex_lock(&qlock);13      pthread_t tid = pthread_self();14      while (constant <10)15      {16          pthread_cond_wait(&qready, &qlock);17      }18      printf("After while thread %u, Constant is %d\n", 19              (unsigned int)tid, constant);20      pthread_mutex_unlock(&qlock);21      pthread_exit((void *)tid);22  }23  24  void *incre(void *arg)25  {26      pthread_mutex_lock(&qlock);27      pthread_t tid = pthread_self();28      while ( ++constant < 20)29      {30          printf("In while thread %u, Constant is %d\n", 31              (unsigned int)tid, constant);32              33          if (constant == 10) {   // 注意這裡34              pthread_mutex_unlock(&qlock);35              pthread_cond_signal(&qready);36              pthread_mutex_lock(&qlock);37          }38      }39      printf("Now the constant is %d\n", constant);40      /*41      constant += 10;42      if (constant == 10)43          pthread_cond_signal(&qready);44      */45      pthread_mutex_unlock(&qlock);46      47      pthread_exit((void *)tid);48  }49  50  int main()51  {52      int err;53      pthread_t tid0, tid1;54  55      err = pthread_create(&tid0, NULL, prints, NULL);56      if (err != 0)57      {58          printf("can't create thread: %d \n", strerror(err));59          exit(0);60      }61      62      sleep(1);63      err = pthread_create(&tid1, NULL, incre, NULL);64      if (err != 0)65      {66          printf("can't create thread: %d \n", strerror(err));67          exit(0);68      }69      sleep(1);70      71      exit(0);72  }

View Code

運行結果1

  原始碼2
 1 #include <stdio.h> 2  #include <pthread.h> 3  #include <stdlib.h> 4  #include <unistd.h> 5   6  pthread_cond_t qready = PTHREAD_COND_INITIALIZER; 7  pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER; 8  int constant = 0; 9  10  void *prints(void *arg)11  {12      pthread_mutex_lock(&qlock);13      pthread_t tid = pthread_self();14      while (constant <10)15      {16                  17          pthread_cond_wait(&qready, &qlock);18      }19      printf("After while thread %u, Constant is %d\n", 20              (unsigned int)tid, constant);21      pthread_mutex_unlock(&qlock);22      pthread_exit((void *)tid);23  }24  25  void *incre(void *arg)26  {27      pthread_mutex_lock(&qlock);28      pthread_t tid = pthread_self();29      while ( ++constant < 20)30      {31          printf("In while thread %u, Constant is %d\n", 32              (unsigned int)tid, constant);33              34          if (constant == 10) {    // 注意這裡35      //        pthread_mutex_unlock(&qlock);36              pthread_cond_signal(&qready);37      //        pthread_mutex_lock(&qlock);38          }39      }40      printf("Now the constant is %d\n", constant);41      /*42      constant += 10;43      if (constant == 10)44          pthread_cond_signal(&qready);45      */46      pthread_mutex_unlock(&qlock);47      48      pthread_exit((void *)tid);49  }50  51  int main()52  {53      int err;54      pthread_t tid0, tid1;55  56      err = pthread_create(&tid0, NULL, prints, NULL);57      if (err != 0)58      {59          printf("can't create thread: %d \n", strerror(err));60          exit(0);61      }62      63      sleep(1);64      err = pthread_create(&tid1, NULL, incre, NULL);65      if (err != 0)66      {67          printf("can't create thread: %d \n", strerror(err));68          exit(0);69      }70      sleep(1);71      72      exit(0);73  }

View Code運行結果:   其中,雖然函數pthread_wait_cond()之前,該線程已經持有鎖。但是在該函數阻塞時,將釋放掉鎖。因此,只有只有鎖的線程釋放掉,才能繼續從pthread_wait_cond()之後執行,因為該函數將再次獲得鎖的控制權。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.