条件变量

 1 #include <stdio.h>
 2 #include <pthread.h>
 3 
 4 pthread_mutex_t mutex;
 5 pthread_cond_t cond;
 6 
 7 void *thread1(void *arg) 
 8 {
 9     pthread_cleanup_push(pthread_mutex_unlock,&mutex);
/*不论是可预见的线程终止还是异常终止,都会存在资源释放的问题,在不考虑因运行出错而退出的前提下,如何保证线程终止时能顺利的释放掉自己所占用的资源,特别是锁资源,就是一个必须考虑解决的问题。最经常出现的情形是资源独占锁的使用:线程为了访问临界资源而为其加上锁,但在访问过程中被外界取消,如果线程处于响应取消状态,且采用异步方式响应,或者在打开独占锁以前的运行路径上存在取消点,则该临界资源将永远处于锁定状态得不到释放。外界取消操作是不可预见的,因此的确需要一个机制来简化用于资源释放的编程。*/
10 //提供函数回调保护 11 while (1) 12 { 13 printf("thread1 is running\n"); 14 pthread_mutex_lock(&mutex); 15 pthread_cond_wait(&cond, &mutex); 16 printf("thread1 applied the condition\n"); 17 pthread_mutex_unlock(&mutex); 18 sleep(4); 19 } 20 pthread_cleanup_pop(0); 21 22 } 23 24 void *thread2(void *arg) 25 { 26 while (1) 27 { 28 printf("thread2 is running\n"); 29 pthread_mutex_lock(&mutex); 30 pthread_cond_wait(&cond, &mutex); 31 printf("thread2 applied the condition\n"); 32 pthread_mutex_unlock(&mutex); 33 sleep(1); 34 } 35 } 36 37 int main() 38 { 39 pthread_t thid1, thid2; 40 printf("condition variable study!\n"); 41 pthread_mutex_init(&mutex, NULL);//注册互斥量 42 pthread_cond_init(&cond, NULL);//注册条件变量 43 pthread_create(&thid1, NULL, (void *) thread1, NULL); 44 pthread_create(&thid2, NULL, (void *) thread2, NULL); 45 46 do { 47 pthread_cond_signal(&cond); 48 } while (1); 49 50 sleep(10); 51 pthread_exit(0); 52 return 0; 53 }

程序运行结果:

 1 condition variable study!
 2 thread2 is running
 3 thread1 is running
 4 thread2 applied the condition
 5 thread1 applied the condition
 6 thread2 is running
 7 thread2 applied the condition
 8 thread2 is running
 9 thread2 applied the condition
10 thread2 is running
11 thread2 applied the condition
12 thread1 is running
13 thread1 applied the condition
14 thread2 is running
15 thread2 applied the condition
16 thread2 is running
17 thread2 applied the condition
18 thread2 is running
19 thread2 applied the condition
20 thread2 is running
21 thread2 applied the condition
22 thread1 is running
23 thread1 applied the condition
24 thread2 is running
25 thread2 applied the condition
26 thread2 is running
27 thread2 applied the condition
28 thread2 is running
29 thread2 applied the condition
30 thread2 is running
31 thread2 applied the condition
32 thread1 is running
33 thread1 applied the condition
34 thread2 is running
35 thread2 applied the condition
36 thread2 is running
37 thread2 applied the condition
38 thread2 is running
39 thread2 applied the condition
40 thread2 is running
41 thread2 applied the condition
42 thread1 is running
43 thread1 applied the condition
44 thread2 is running
45 thread2 applied the condition
46 thread2 is running
47 thread2 applied the condition
48 thread2 is running
49 thread2 applied the condition
50 thread2 is running
51 thread2 applied the condition
52 thread1 is running
53 thread1 applied the condition
54 thread2 is running
55 thread2 applied the condition
56 thread2 is running
57 thread2 applied the condition
58 thread2 is running
59 thread2 applied the condition
60 thread2 is running
61 thread2 applied the condition
62 thread1 is running
63 thread1 applied the condition
64 thread2 is running
65 thread2 applied the condition

你可能感兴趣的:(条件变量)