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
|
#include <common.h>
#include <pthread.h>
#define NUMCHUNKS 1000
typedef union {
unsigned char cp;
int count;
} data_t;
static int producer(t_iemnet_queue*q, unsigned int count, unsigned int msec) {
unsigned int i;
data_t data;
for(i=0; i<count; i++) {
t_iemnet_chunk*chunk=0;
data.count=i;
chunk=iemnet__chunk_create_data(sizeof(data), &data.cp);
queue_push(q, chunk);
usleep(1000*msec);
}
return 0;
}
static int consumer(t_iemnet_queue*q) {
t_iemnet_chunk*chunk=NULL;
while(1) {
data_t*data=NULL;
chunk=queue_pop_block(q);
if(!chunk)
break;
if(sizeof(data_t)!=chunk->size) {
error("size mismatch %d!=%d", sizeof(data_t), chunk->size);
fail();
}
data=chunk->data;
//printf("%d ", data->count);
iemnet__chunk_destroy(chunk);
}
printf("\n");
return 0;
}
static void* consumer_thread(void*qq) {
t_iemnet_queue*q=(t_iemnet_queue*)qq;
consumer(q);
return NULL;
}
void threadedqueue_setup(void) {
pthread_t thread;
pthread_attr_t threadattr;
t_iemnet_queue*q=queue_create();
/* prepare child thread */
if(pthread_attr_init(&threadattr) < 0) {
error("warning: could not prepare child thread");
fail();
}
if(pthread_attr_setdetachstate(&threadattr, PTHREAD_CREATE_DETACHED) < 0) {
error("warning: could not prepare child thread...");
fail();
}
if(pthread_create(&thread, &threadattr, consumer_thread, q) < 0) {
error("warning: could not create child thread");
fail();
}
producer(q, 1000, 1);
queue_destroy(q);
pass();
}
|