-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathpthread_cond.c
More file actions
73 lines (52 loc) · 1.57 KB
/
Copy pathpthread_cond.c
File metadata and controls
73 lines (52 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
static pthread_cond_t cond_1 = PTHREAD_COND_INITIALIZER;
char tmp[100];
static void cleanup_handler(void *arg)
{
char* ptr = (char*)arg;
pthread_mutex_unlock(&mtx);
}
static void cleanup_handler_1(void *arg)
{
char* ptr = (char*)arg;
pthread_mutex_unlock(&mtx);
}
static void *thread_func(void *arg)
{
pthread_cleanup_push(cleanup_handler, tmp);
pthread_mutex_lock(&mtx);
pthread_cond_wait(&cond, &mtx);
pthread_mutex_unlock(&mtx);
pthread_cleanup_pop(0);
return NULL;
}
static void *thread_func_1(void *arg)
{
pthread_cleanup_push(cleanup_handler_1, tmp);
printf("B: START\n");
pthread_mutex_lock(&mtx);
pthread_cond_wait(&cond_1, &mtx); //你會發現有cond_1 ,mtx ,在wait時 , 會將mtx release ,意味其他的thread可以做pthread_mutex_lock
pthread_mutex_unlock(&mtx);
pthread_cleanup_pop(0);
return NULL;
}
int main(void)
{
int i;
pthread_t tid;
pthread_t tid_1;
pthread_create(&tid , NULL, thread_func , NULL);
pthread_create(&tid_1, NULL, thread_func_1, NULL);
pthread_mutex_lock(&mtx);
pthread_mutex_unlock(&mtx);
pthread_cond_signal(&cond);
pthread_cancel(tid);
pthread_cancel(tid_1);
pthread_join(tid, NULL);
return 0;
}