-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfifo_test.c
94 lines (72 loc) · 1.86 KB
/
fifo_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
#include <stdio.h>
#include "queue_ts.h"
#define number_of_threads 6
#define number_of_threads_d 5
#define number_of_iters 1000
#define DEBUG
//#undef DEBUG /* uncomment when you are done! */
#ifdef DEBUG
#define print printf
#else
#define print(...)
#endif
struct _stru {
int number;
int thread_no;
queue * q;
};
void * func(void * arg)
{
struct _stru * args = (struct _stru *) arg;
int number = args->number;
//int th_number = args->thread_no;
queue * q = args->q;
int i;
srand(time(NULL));
for(i = 0; i < number; i++) {
char * message = malloc(16);
snprintf(message, 15, "rand: %d", rand());
enque(q, (void *)message);
}
return NULL;
}
void * func_d(void * args)
{
print("Func_D running....\n");
queue * q = (queue *) args;
void * data;
while((data = deque(q)) != NULL) {
char * string = (char *)data;
print("DeQued : %s, @%p\n", string, data);
free(data);
}
print("Func_D exiting....\n");
return NULL;
}
int main(void)
{
queue * q = queue_factory();
pthread_t threads[number_of_threads];
pthread_t thread_d[number_of_threads_d];
int i;
struct _stru arg[number_of_threads];
for(i = 0; i < number_of_threads; i++) {
arg[i].number = number_of_iters;
arg[i].thread_no = i;
arg[i].q = q;
pthread_create(threads+i, NULL, func, (void *)&arg[i]);
}
for(i = 0; i < number_of_threads_d; i++) {
pthread_create(thread_d+i, NULL, func_d, (void *)q);
}
for(i = 0; i < number_of_threads; i++) {
pthread_join(*(threads+i), NULL);
}
for(i = 0; i < number_of_threads_d; i++) {
pthread_join(*(thread_d+i), NULL);
}
print("Going to run queue_destroy(...) \n");
queue_destroy(q);
print("que freeed... :) \n");
return 0;
}