-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtmp_test.c
More file actions
38 lines (30 loc) · 707 Bytes
/
Copy pathtmp_test.c
File metadata and controls
38 lines (30 loc) · 707 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_cond_t thread_cond;
pthread_mutex_t thread_lock;
static void *work_thread(void *arg)
{
pthread_cond_signal(&thread_cond);
sleep(10);
pthread_cond_signal(&thread_cond);
}
int main(void)
{
int err;
pthread_t tId;
pthread_cond_init(&thread_cond, NULL);
pthread_mutex_init(&thread_lock, NULL);
err = pthread_create(&tId, NULL, work_thread, NULL);
if (0 != err) {
perror("create work thread failed.\n");
return -1;
}
sleep(5);
printf("wait condition\n");
pthread_mutex_lock(&thread_lock);
pthread_cond_wait(&thread_cond, &thread_lock);
pthread_mutex_unlock(&thread_lock);
printf("condition ready\n");
return 0;
}