-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreaded_main.c
74 lines (54 loc) · 1.45 KB
/
threaded_main.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
71
72
73
74
/*-------------------------------------------------------------------
Author: Aaron Anthony Valoroso
Date: November 3rd, 2019
License: None
Email: Aaron.A.Valoroso@erdc.dren.mil
To Run (Type): make threads
--------------------------------------------------------------------*/
#include "queue.h"
queue * q = NULL;
queue * q2 = NULL;
void * worker_threads() {
int number = 0;
while(1) {
if (queue_size(q) > 0) {
number = pop(q);
printf("Looking at number: %d\n", number);
} else {
break;
}
}
return NULL;
}
void * worker_threads2() {
int number = 0;
while(1) {
if (queue_size(q2) > 0) {
number = pop(q2);
printf("Looking at number2: %d\n", number);
} else {
break;
}
}
return NULL;
}
int main() {
q = create_queue();
q2 = create_queue();
pthread_t tid[4];
for(int i = 0; i < 5; ++i)
push(q, i * 10);
for(int i = 0; i < 5; ++i)
push(q2, i * 20);
pthread_create(&tid[0], NULL, worker_threads, NULL);
pthread_create(&tid[1], NULL, worker_threads, NULL);
pthread_create(&tid[2], NULL, worker_threads2, NULL);
pthread_create(&tid[3], NULL, worker_threads2, NULL);
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
pthread_join(tid[2], NULL);
pthread_join(tid[3], NULL);
queue_cleanup(q);
queue_cleanup(q2);
return 0;
}