-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
executable file
·121 lines (93 loc) · 2.24 KB
/
test.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "mtmq.h"
#include <stdio.h>
#include <pthread.h>
#include <signal.h>
#include <unistd.h>
static mtmq_t *queue;
static volatile int s_break;
void sig_handler(int n) {
s_break = 1;
return;
}
static void *thread_wr(void *data);
static void *thread_rd(void *data);
int main()
{
#ifdef _WIN32
signal(SIGINT, sig_handler);
#else
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = sig_handler;
sigaction(SIGINT, &sa, NULL);
#endif
printf("Press Ctrl-C to exit.\n");
queue = mtmq_create(5);
if (!queue) {
printf("mtmq_create() error\n");
return 1;
}
pthread_t tid_wr1;
pthread_t tid_rd1;
pthread_create(&tid_wr1, NULL, thread_wr, NULL);
pthread_create(&tid_rd1, NULL, thread_rd, NULL);
while (!mtmq_is_finalized(queue)) {
sleep(1);
if (s_break) {
printf("Got SIGINT signal.\n");
mtmq_finalize(queue);
break;
}
}
pthread_join(tid_wr1, NULL);
pthread_join(tid_rd1, NULL);
int rc = mtmq_destroy(queue);
if (rc != MTMQ_RC_OK) {
printf("mtmq_destroy() error\n");
return 1;
}
return 0;
}
static void *thread_wr(void *data)
{
(void)data;
for (int i=0; i<16; i++) {
int rc = mtmq_push(queue, i, NULL, -1);
if (rc == MTMQ_RC_FINALIZED) {
printf("Producer: queue is finalized.\n");
break;
} else if (rc != MTMQ_RC_OK) {
printf("Error in mtmq_push().\n");
break;
}
}
mtmq_finalize(queue);
printf("Producer exiting.\n");
return NULL;
}
static void *thread_rd(void *data)
{
int i;
void *payload;
(void)data;
for (;;) {
sleep(1);
int rc = mtmq_pop(queue, &i, &payload, 250);
if (rc == MTMQ_RC_FINALIZED) {
printf("Consumer: queue is finalized.\n");
break;
}
if (rc == MTMQ_RC_TIMEDOUT) {
printf("Consumer: timeout reading from queue.\n");
continue;
}
if (rc != MTMQ_RC_OK) {
printf("Error in mtmq_pop().\n");
break;
}
printf("%d\n", i);
}
printf("Consumer exiting.\n");
return NULL;
}