Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleFiFoQueue.cpp
1 #include <Base/Common/FiFoQueue.hh>
2 #include <iostream>
3 
4 using namespace std;
5 using namespace BIAS;
6 
7 pthread_mutex_t* mut;
8 int arr[4000000];
9 
10 void* reader(void* arg) {
11  FiFoQueue<int>* queue= (FiFoQueue<int>*)arg;
12  int a;
13  //fetch lots of data
14  for (size_t i=0; i<1000000; i++) {
15  queue->FetchSynced(a);
16  //ensure fetched data was not fetched before
17  BIASASSERT(arr[a]!=1);
18  arr[a] = 1;
19  }
20  return NULL;
21 }
22 
23 void* writer1(void* arg) {
24  FiFoQueue<int>* queue= (FiFoQueue<int>*)arg;
25  for (int i=0; i<1000000; i++) {
26  queue->AppendSynced(i);
27  //cout << "w1: " << i << endl << flush;
28  }
29  return NULL;
30 }
31 
32 void* writer2(void* arg) {
33  FiFoQueue<int>* queue= (FiFoQueue<int>*)arg;
34  for (int i=1000000; i<2000000; i++) {
35  queue->AppendSynced(i);
36  //cout << "w2: " << i << endl << flush;
37  }
38  return NULL;
39 }
40 
41 void* writer3(void* arg) {
42  FiFoQueue<int>* queue= (FiFoQueue<int>*)arg;
43  for (int i=2000000; i<3000000; i++) {
44  queue->AppendSynced(i);
45  //cout << "w2: " << i << endl << flush;
46  }
47  return NULL;
48 }
49 
50 void* writer4(void* arg) {
51  FiFoQueue<int>* queue= (FiFoQueue<int>*)arg;
52  for (int i=3000000; i<4000000; i++) {
53  queue->AppendSynced(i);
54  //cout << "w2: " << i << endl << flush;
55  }
56  return NULL;
57 }
58 
59 
60 int main()
61 {
62  //create a queue with 20 slots
63  FiFoQueue<int>* queue = new FiFoQueue<int>(1);
64 
65  //debugging array, ensure that every data is read only once
66  mut = new pthread_mutex_t();
67  pthread_mutex_init(mut,NULL);
68  for (int i=0; i<4000000; i++)
69  arr[i] = 0;
70 
71  //create 4 reader and 4 writers
72  pthread_t threadId1,threadId2,threadId3,threadId4,
73  threadId5,threadId6,threadId7,threadId8;
74  pthread_create(&threadId1,NULL,&reader,(void*)queue);
75  pthread_create(&threadId2,NULL,&reader,(void*)queue);
76  pthread_create(&threadId3,NULL,&reader,(void*)queue);
77  pthread_create(&threadId4,NULL,&reader,(void*)queue);
78  pthread_create(&threadId5,NULL,&writer1,(void*)queue);
79  pthread_create(&threadId6,NULL,&writer2,(void*)queue);
80  pthread_create(&threadId7,NULL,&writer3,(void*)queue);
81  pthread_create(&threadId8,NULL,&writer4,(void*)queue);
82 
83  //wait for threads
84  pthread_join(threadId1,NULL);
85  pthread_join(threadId2,NULL);
86  pthread_join(threadId3,NULL);
87  pthread_join(threadId4,NULL);
88  pthread_join(threadId5,NULL);
89  pthread_join(threadId6,NULL);
90  pthread_join(threadId7,NULL);
91  pthread_join(threadId8,NULL);
92 
93  delete queue;
94 
95  return 0;
96 }
This class implements a templated standard FiFo-queue for inter process communication (arbitrary numb...
Definition: FiFoQueue.hh:60
int FetchSynced(T &item)
Fetch the oldest item from the queue.
Definition: FiFoQueue.hh:203
int AppendSynced(T &item)
Append an datum as newest item.
Definition: FiFoQueue.hh:136