-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_obqueue_no_blocking.c
93 lines (78 loc) · 2.66 KB
/
test_obqueue_no_blocking.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
#include "obqueue_no_blocking.h"
#define DUMMY_VALUE 2341321
long COUNTS_PER_THREAD = 2500000;
int threshold = 8;
obqueue_t qq;
pthread_barrier_t pro_barrier;
int* array;
void* produce_and_consume(void* index) {
obqueue_t* q = &qq;
handle_t* th = (handle_t*) malloc(sizeof(handle_t));
memset(th, 0, sizeof(handle_t));
ob_queue_register(q, th, ENQ | DEQ);
for(;; ) {
pthread_barrier_wait(&pro_barrier);
int i = 0;
for(; i < COUNTS_PER_THREAD; ++i) {
ob_enqueue(q, th, 1 + i + ((int) index) * COUNTS_PER_THREAD);
int value;
if((value = ob_dequeue(q, th)) == NULL)
return NULL;
array[value] = 1;
}
pthread_barrier_wait(&pro_barrier);
}
return NULL;
}
#define THREAD_NUM 4
int main(int argc, char* argv[]) {
printf("thread number: %d\n", THREAD_NUM);
pthread_barrier_init(&pro_barrier, NULL, THREAD_NUM + 1);
printf("cpu: %d\n", get_nprocs_conf());
if(argc >= 3) {
COUNTS_PER_THREAD = atol(argv[1]);
threshold = atoi(argv[2]);
}
printf("here %ld\n", THREAD_NUM * COUNTS_PER_THREAD);
array = (int*) malloc((1 + THREAD_NUM * COUNTS_PER_THREAD) * sizeof(int));
memset(array, 0, (1 + THREAD_NUM * COUNTS_PER_THREAD) * sizeof(int));
fflush(stdout);
ob_init_queue(&qq, THREAD_NUM, THREAD_NUM, threshold);
pthread_t pids[THREAD_NUM];
int i = 0;
for(; i < THREAD_NUM; ++i) {
if(-1 == pthread_create(&pids[i], NULL, produce_and_consume, i)) {
printf("error create thread\n");
exit(1);
}
}
i = 0;
for(; i < 8;) {
printf("\n%d times\n", i);
sleep(1);
struct timeval start;
gettimeofday(&start, NULL);
pthread_barrier_wait(&pro_barrier);
pthread_barrier_wait(&pro_barrier);
struct timeval pro_end;
gettimeofday(&pro_end, NULL);
int verify = 1;
int j = 1;
for(; j <= THREAD_NUM * COUNTS_PER_THREAD; ++j) {
if(array[j] != 1) {
printf("Error: ints[%d]\n", j);
verify = 0;
break;
}
}
if(verify)
printf("ints[1-%ld] has been Verify through\n", THREAD_NUM * COUNTS_PER_THREAD);
float cost_time = (pro_end.tv_sec-start.tv_sec)+(pro_end.tv_usec-start.tv_usec) / 1000000.0;
printf("pro&con cost times: %f seconds\n", cost_time);
printf("%d times over\n", i);
fflush(stdout);
memset(array, 0, (1 + THREAD_NUM * COUNTS_PER_THREAD) * sizeof(int));
++i;
}
return 0;
}