-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample2.c
More file actions
135 lines (111 loc) · 2.33 KB
/
sample2.c
File metadata and controls
135 lines (111 loc) · 2.33 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct QueueEntry {
char* string;
int empty;
int timestamp;
};
#define Q_SIZE 3
struct QueueEntry Q[Q_SIZE];
pthread_mutex_t Q_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t signal_mutex = PTHREAD_MUTEX_INITIALIZER;
int g_timestamp = 0;
void Q_init();
void Q_clear();
void Q_add(const char*);
void* thread1(void* arg);
void* thread2(void* arg);
int main()
{
Q_init();
pthread_t thread;
pthread_create(&thread, NULL, thread2, NULL);
thread1(0);
pthread_join(thread, NULL);
return 0;
}
void* thread1(void* arg)
{
Q_add("data 0");
Q_add("data 1");
Q_add("data 2");
Q_add("data 3");
Q_add("data 4");
Q_clear();
return NULL;
}
void* thread2(void* arg)
{
Q_add("data 5");
Q_add("data 6");
Q_add("data 7");
Q_add("data 8");
Q_add("data 9");
Q_clear();
return NULL;
}
void Q_init()
{
int i;
for (i = 0; i < Q_SIZE; i++) {
Q[i].string = 0;
Q[i].empty = 1;
Q[i].timestamp = 0;
}
}
void Q_clear()
{
Q_init();
}
int Q_full()
{
pthread_mutex_lock(&Q_mutex);
int idx;
int ret = 1;
for (idx = 0; idx < Q_SIZE; idx++)
if (Q[idx].empty) ret = 0;
pthread_mutex_unlock(&Q_mutex);
return ret;
}
void Q_discard_oldest()
{
int discardidx;
int discardts;
int idx;
pthread_mutex_lock(&Q_mutex);
discardidx = 0;
discardts = Q[0].timestamp;
for (idx = 1; idx < Q_SIZE; idx++)
if (discardts > Q[idx].timestamp) {
discardidx = idx;
discardts = Q[idx].timestamp;
}
fputs ("discard ", stdout);
fputs (Q[discardidx].string, stdout);
fputs ("\n", stdout);
free(Q[discardidx].string);
Q[discardidx].empty = 1;
Q[discardidx].string = 0;
Q[discardidx].timestamp = 0;
pthread_mutex_unlock(&Q_mutex);
}
void Q_add(const char* s)
{
if (Q_full())
Q_discard_oldest();
pthread_mutex_lock(&Q_mutex);
int idx;
for (idx = 0; idx < Q_SIZE; idx++) {
if (Q[idx].empty) break;
}
Q[idx].empty = 0;
Q[idx].timestamp = ++g_timestamp;
Q[idx].string = strdup(s);
fputs ("add ", stdout);
fputs (Q[idx].string, stdout);
fputs ("\n", stdout);
pthread_mutex_unlock(&Q_mutex);
}