【Linux 编程】pthead_cond_t 的使用

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

  【Linux 编程】pthead_cond_t 的使用
源代码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
运行结果
【Linux 编程】pthead_cond_t 的使用
 
  其中,虽然函数pthread_wait_cond()之前,该线程已经持有锁。但是在该函数阻塞时,将释放掉锁。因此,只有只有锁的线程释放掉,才能继续从pthread_wait_cond()之后执行,因为该函数将再次获得锁的控制权。

你可能感兴趣的:(linux)