forked from dodng/fast_ring_queue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify_main.c
119 lines (106 loc) · 2.88 KB
/
notify_main.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
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
/*from dodng
2016.12.9*/
#include <stdio.h>
#include "ring_queue.h"
#include <unistd.h>
#include <sys/time.h>
const int LOOP_SIZE = 10000;
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#define THREAD_NUM 1
#define DATA_TYPE int
void *customer(void *arg)
{
Ring_Queue queue = *(Ring_Queue *)arg;
u_char get_num = 0;
while(1)
{
for(int i = 0;i < LOOP_SIZE; )
{
int *p = 0;
//begin--for notify
recv(queue.notify_fd[1], &get_num, sizeof(get_num), 0);
for(int j = 0 ; j < get_num;j++)
//end----for notify
{
p = (DATA_TYPE *)queue.SOLO_Read();
if (p)
{
assert(i == *p);
// printf("[%d]:%d\n",i,*p);
queue.SOLO_Read_Over();
i++;
}
}
}
}
}
void *producer(void *arg)
{
Ring_Queue queue = *(Ring_Queue *)arg;
int loop = 0;
struct timeval last_time,now_time;
gettimeofday(&last_time,NULL);
gettimeofday(&now_time,NULL);
u_char wrote_num = 0;
while(1)
{
for(int i = 0;i < LOOP_SIZE; )
{
int *p = 0;
p = (DATA_TYPE *)queue.SOLO_Write();
if (p)
{
*p = i;
queue.SOLO_Write_Over();
i++;
//begin--for notify
wrote_num++;
if (wrote_num >= 200)
{
send(queue.notify_fd[0], &wrote_num, sizeof(wrote_num), MSG_NOSIGNAL);
wrote_num = 0;
}
//end----for notify
}
}
gettimeofday(&now_time,NULL);
if (now_time.tv_sec - last_time.tv_sec >= 5)
{
printf("LOOP_COUNT is %.2f=[ %d[RING_SIZE] * %.2f[RING_COUNT]] per second\n",(LOOP_SIZE*loop)/5.0,LOOP_SIZE,loop/5.0);
last_time = now_time;
loop = 0;
}
loop++;
}
}
int main(int argc,char *argv[])
{
pthread_t tid_customer[THREAD_NUM];
pthread_t tid_producer[THREAD_NUM];
Ring_Queue *queue = new Ring_Queue[THREAD_NUM](LOOP_SIZE,sizeof(DATA_TYPE));
for (int i = 0; i < THREAD_NUM; i++)
{
if (pthread_create(&tid_customer[i],NULL,&customer,(void*)&queue[i]) != 0)
{
fprintf(stderr,"thread create failed\n");
return -1;
}
}
for (int i = 0; i < THREAD_NUM; i++)
{
if (pthread_create(&tid_producer[i],NULL,&producer,(void*)&queue[i]) != 0)
{
fprintf(stderr,"thread create failed\n");
return -1;
}
}
for (int i = 0 ;i < THREAD_NUM; i++)
pthread_join(tid_customer[i],NULL);
for (int i = 0 ;i < THREAD_NUM; i++)
pthread_join(tid_producer[i],NULL);
}