-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
51 lines (43 loc) · 1.07 KB
/
test.c
File metadata and controls
51 lines (43 loc) · 1.07 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
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include "rqueue.h"
struct rqueue *rqueue = NULL;
void *
func_enqueue(void *arg)
{
long i;
int num = atoi((char *)arg);
for (i = 1; i <= num; i++) {
while(RQUEUE_QUEUE_FULL == rqueue_enqueue(rqueue, (void *)(long)i)){
}
}
return NULL;
}
void *
func_dequeue(void *arg)
{
int i = 0;
long item = 0;
int num = atoi((char *)arg);
for (i = 1; i <= num; i++) {
while(RQUEUE_QUEUE_EMPTY == rqueue_dequeue(rqueue, (void **)&item)){
}
printf(" [%d:%ld]", i, item);
}
return NULL;
}
int main(int argc, char *argv[])
{
pthread_t thd1, thd2;
assert(argc == 3);
assert(atoi(argv[1]) >= atoi(argv[2]));
rqueue = rqueue_init(100);
printf ("enqueue %d\n", atoi(argv[1]));
printf ("dequeue %d\n", atoi(argv[2]));
pthread_create(&thd1, NULL, func_enqueue, argv[1]);
pthread_create(&thd2, NULL, func_dequeue, argv[2]);
pthread_join(thd1, NULL);
pthread_join(thd2, NULL);
return printf("\n==>remain %u-%u %u\n", (unsigned int)(rqueue->head), (unsigned int)(rqueue->tail), (unsigned int)(rqueue->head - rqueue->tail));
}