-
Notifications
You must be signed in to change notification settings - Fork 0
/
13_simple_condvar.c
70 lines (62 loc) · 1.33 KB
/
13_simple_condvar.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <sched.h>
struct condvar {
pthread_mutex_t event_lock;
bool is_event_set;
};
void
condvar_signal(struct condvar *condvar)
{
printf("condvar signaled\n");
condvar->is_event_set = true;
pthread_mutex_unlock(&condvar->event_lock);
pthread_mutex_lock(&condvar->event_lock);
condvar->is_event_set = false;
printf("condvar is locked\n");
}
void
condvar_wait(struct condvar *condvar)
{
for (;;) {
pthread_mutex_lock(&condvar->event_lock);
if (condvar->is_event_set) {
condvar->is_event_set = false;
pthread_mutex_unlock(&condvar->event_lock);
return;
}
pthread_mutex_unlock(&condvar->event_lock);
}
}
void
condvar_create(struct condvar *condvar)
{
pthread_mutex_init(&condvar->event_lock, NULL);
pthread_mutex_lock(&condvar->event_lock);
condvar->is_event_set = false;
}
struct condvar condvar;
void *
thread_f(void *arg)
{
int id = (int) arg;
for (;;) {
condvar_wait(&condvar);
printf("%d processed the event\n", id);
}
}
int
main()
{
condvar_create(&condvar);
const int thread_count = 10;
pthread_t tid[thread_count];
for (int i = 0; i < thread_count; ++i)
pthread_create(&tid[i], NULL, thread_f, (void *) i);
while (getchar() != EOF)
condvar_signal(&condvar);
for (int i = 0; i < thread_count; ++i)
pthread_join(tid[i], NULL);
return 0;
}