-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsimple-cond.c
48 lines (35 loc) · 855 Bytes
/
simple-cond.c
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
#include <stdlib.h>
#include <stdio.h>
#include <assert.h> // assert
#include <unistd.h> // sleep
#include <string.h> // memset
#include "uv.h"
typedef struct {
uv_mutex_t mutex;
uv_cond_t cond;
int delay;
int use_broadcast;
volatile int posted;
} worker_config;
static void worker(void* arg) {
printf("worker\n");
worker_config* wc = (worker_config*) arg;
uv_mutex_lock(&wc->mutex);
uv_cond_signal(&wc->cond);
uv_mutex_unlock(&wc->mutex);
}
int main() {
uv_thread_t thread;
worker_config wc;
memset(&wc, 0, sizeof(wc));
uv_cond_init(&wc.cond);
uv_mutex_init(&wc.mutex);
uv_thread_create(&thread, worker, &wc);
uv_mutex_lock(&wc.mutex);
uv_cond_wait(&wc.cond, &wc.mutex);
uv_mutex_unlock(&wc.mutex);
uv_thread_join(&thread);
uv_mutex_destroy(&wc.mutex);
uv_cond_destroy(&wc.cond);
return 0;
}