-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcondition-variable.c
More file actions
89 lines (80 loc) · 1.84 KB
/
condition-variable.c
File metadata and controls
89 lines (80 loc) · 1.84 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "queue.h"
pthread_t tid1, tid2, tid3;
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
typedef struct Task
{
SIMPLEQ_ENTRY(Task) simpleq;
int value;
} Task;
typedef SIMPLEQ_HEAD(Task_Q, Task) Task_Q;
static Task_Q queue;
int loop = 1;
void destroy_task_queues()
{
Task * task;
while ((task = SIMPLEQ_FIRST(&queue)) != NULL)
{
SIMPLEQ_REMOVE_HEAD(&queue, simpleq);
free(task);
}
}
void* foo()
{
Task * task;
pthread_mutex_lock(&lock);
while (loop)
{
task = SIMPLEQ_FIRST(&queue);
if (task)
{
SIMPLEQ_REMOVE_HEAD(&queue, simpleq);
printf("getting: %d\n", task->value);
free(task);
}
else
{
printf("waiting\n");
pthread_cond_wait(&cond1, &lock);
}
}
pthread_mutex_unlock(&lock);
printf("consumer end\n");
return NULL;
}
void * goo(void * arg)
{
for (int i = 0; i < 40; ++i)
{
pthread_mutex_lock(&lock);
Task * t = malloc(sizeof(Task));
t->value = ((int) arg) + i;
// printf("Signaling %d\n", t->value);
SIMPLEQ_INSERT_TAIL(&queue, t, simpleq);
pthread_cond_signal(&cond1);
pthread_mutex_unlock(&lock);
// sleep(1);
}
printf("producer %d end\n", (int) arg);
return NULL;
}
int main()
{
SIMPLEQ_INIT(&queue);
pthread_create(&tid1, NULL, foo, NULL);
pthread_create(&tid2, NULL, goo, 100);
pthread_create(&tid3, NULL, goo, 200);
pthread_join(tid2, NULL);
pthread_join(tid3, NULL);
sleep(1);
loop = 0;
pthread_cond_signal(&cond1);
printf("main end\n");
pthread_join(tid1, NULL);
destroy_task_queues();
return 0;
}