互斥锁mutex
1#include
互斥锁用于上锁,条件变量用于等待
条件变量cond
1 #include
2 #include
3 #include
4 /*
5 *pthread_cond_t*
6 *pthread_cond_init()*
7 *pthread_cond_destory()*
8 *pthread_cond_wait()//一直等待,为等待+减锁,一直等待下去
9 *pthread_cond_timedwait()//定时等待,允许线程就阻塞时间设置一个限制值
10 *pthread_cond_signal()//为加锁+ 不等待,直接加锁,唤醒,谁先阻塞,只唤醒谁,只唤醒等待在相应条件变量上的一个
线程 但需避免重复加锁
11 *pthread_cond_broadcast()// 唤醒所有阻塞在相应条件变量上的所有进程,一下子全部唤醒
12 * */
13
/*
14 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
15 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
16
17 void* thread_fun1(void *arg)
18 {
19 printf("enter fun1\n");
20 pthread_mutex_lock(&mutex);
21
22 printf("This is fun1.\n");
23 printf("fun1 sleep...\n");
24 sleep(12);
25 //pthread_cond_wait(&cond, &mutex);
26 pthread_cond_timedwait(&cond, &mutex);
27 printf("fun1 wakeup.\n");
28 pthread_mutex_unlock(&mutex);
29 printf("fun1 unlock.\n");
30 }
31 void* thread_fun2(void *arg)
32 {
33 printf("enter fun2\n");
34 //pthread_mutex_lock(&mutex);
35 //printf("fun2 lock.\n");
36
37 printf("This is fun2.\n");
38 //printf("fun1 sleep........\n");
39 //sleep(2);
40 pthread_cond_signal(&cond);
41 //printf("fun2 sleep....\n");
42 sleep(15);
43 pthread_mutex_unlock(&mutex);
44 printf("fun2 unlock\n");
45 }
46
47 int main()
48 {
49 pthread_t tid1,tid2;
50 pthread_create(&tid1, NULL, thread_fun1, NULL);
51 pthread_create(&tid2, NULL, thread_fun2, NULL);
52
53 pthread_join(tid1, NULL);
54 pthread_join(tid2, NULL);
55 return 0;
56 }
*/
58 #define MAX_SIZE 10
59 static int max_count = MAX_SIZE;
60 static int cur_count = 1;
61
62 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
63 pthread_cond_t OS = PTHREAD_COND_INITIALIZER;
64 pthread_cond_t JS = PTHREAD_COND_INITIALIZER;
65
66 void* A_fun(void *arg)
67 {
68 while(cur_count <= max_count)
69 {
70 pthread_mutex_lock(&mutex);
71 if(cur_count % 2 == 1)
72 {
73 printf("A : %d\n",cur_count);
74 cur_count++;
75 pthread_cond_signal(&JS);
76 }
77 else
78 {
79 pthread_cond_wait(&OS, &mutex);
80 }
81 pthread_mutex_unlock(&mutex);
82 }
83 }
84 void* B_fun(void *arg)
85 {
86 while(cur_count <= max_count)
87 {
88 pthread_mutex_lock(&mutex);
89 if(cur_count % 2 == 0)
90 {
91 printf("B : %d\n",cur_count);
92 cur_count++;
93 pthread_cond_signal(&OS);
94 }
95 else
96 pthread_cond_wait(&JS, &mutex);
97 pthread_mutex_unlock(&mutex);
98 }
99 }
100 int main()
101 {
102 pthread_t ta_id, tb_id;
103 pthread_create(&ta_id, NULL, A_fun, NULL);
104 pthread_create(&tb_id, NULL, B_fun, NULL);
105
106 pthread_join(ta_id, NULL);
107 pthread_join(ta_id, NULL);
108 return 0;
109 }
读/写锁(读锁:共享锁;写锁:独占锁(线对写锁分配,即写锁优先),)
114 pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
115 void* A_fun(void *arg)
116 {
117 pthread_rwlock_wrlock(&rwlock);
118 printf("This is A fun.\n");
119 sleep(10);
120 pthread_rwlock_unlock(&rwlock);
121 }
122 void* B_fun(void *arg)
123 {
124 pthread_rwlock_rdlock(&rwlock);
125 printf("This is B fun.\n");
126 pthread_rwlock_unlock(&rwlock);
127 }
128 void* C_fun(void *arg)
129 {
130 pthread_rwlock_wrlock(&rwlock);
131 printf("This is C fun.\n");
132 pthread_rwlock_unlock(&rwlock);
133 }
134
135 void* D_fun(void *arg)
136 {
137 pthread_rwlock_rdlock(&rwlock);
138 printf("This is D fun.\n");
139 pthread_rwlock_unlock(&rwlock);
140 }
141
142 void* E_fun(void *arg)
143 {
144 pthread_rwlock_wrlock(&rwlock);
145 printf("This is E fun.\n");
146 pthread_rwlock_unlock(&rwlock);
147 }
148 void* F_fun(void *arg)
149 {
150 pthread_rwlock_rdlock(&rwlock);
151 printf("This is F fun.\n");
152 pthread_rwlock_unlock(&rwlock);
153 }
154 int main()
155 {
156 pthread_t ta_id, tb_id, tc_id, td_id, te_id, tf_id;
157 pthread_create(&ta_id, NULL, A_fun, NULL);
158 sleep(2);
159 pthread_create(&tb_id, NULL, B_fun, NULL);
160 pthread_create(&tc_id, NULL, C_fun, NULL);
161 pthread_create(&td_id, NULL, D_fun, NULL);
162 pthread_create(&te_id, NULL, E_fun, NULL);
163 pthread_create(&tf_id, NULL, F_fun, NULL);
164
165 pthread_join(ta_id, NULL);
166 pthread_join(tb_id, NULL);
167 pthread_join(tc_id, NULL);
168 pthread_join(td_id, NULL);
169 pthread_join(te_id, NULL);
170 pthread_join(tf_id, NULL);
171 }