-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
68 lines (52 loc) · 1 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
#define _POSIX_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include "ring.h"
#define SIZE 100
int pool[SIZE];
void* push_func(void *p)
{
struct ring *r = (struct ring *)p;
unsigned int seed;
while(1) {
ring_push(r, &pool[rand_r(&seed)%SIZE]);
usleep(rand_r(&seed)%100000);
}
return NULL;
}
void* pop_func(void *p)
{
struct ring *r = (struct ring *)p;
unsigned int seed;
void *ptr;
while(1) {
ptr = ring_pop(r);
if (ptr)
printf("poped %d\n", *(int*)ptr);
else
printf("poped NULL\n");
usleep(rand_r(&seed)%100000);
}
return NULL;
}
int main(int argc, char *argv[])
{
pthread_t thr_push;
pthread_t thr_pop;
struct ring ring;
int i;
for (i = 0; i < SIZE; ++i) {
pool[i] = i;
}
srand(time(NULL));
ring_init(&ring, 2);
pthread_create(&thr_push, NULL, push_func, &ring);
pthread_create(&thr_pop, NULL, pop_func, &ring);
pthread_join(thr_push, NULL);
pthread_join(thr_pop, NULL);
ring_destroy(&ring);
return 0;
}