aboutsummaryrefslogtreecommitdiff
path: root/src/tests/synchroqueue.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-07-18 06:45:17 +0000
committerMike Buland <eichlan@xagasoft.com>2012-07-18 06:45:17 +0000
commit364ded646ad4de2065cdf0a2f4282c7eaf7148e6 (patch)
tree55e1a3a7a9b8eb0a9d65477d4b4a857e267e590f /src/tests/synchroqueue.cpp
parentc9e94c513a917ab997c5f766a00e68f98a5aa6fc (diff)
downloadlibbu++-364ded646ad4de2065cdf0a2f4282c7eaf7148e6.tar.gz
libbu++-364ded646ad4de2065cdf0a2f4282c7eaf7148e6.tar.bz2
libbu++-364ded646ad4de2065cdf0a2f4282c7eaf7148e6.tar.xz
libbu++-364ded646ad4de2065cdf0a2f4282c7eaf7148e6.zip
Bu::SynchroQueue had a bug, it seems to be all fixed now.
Diffstat (limited to 'src/tests/synchroqueue.cpp')
-rw-r--r--src/tests/synchroqueue.cpp131
1 files changed, 131 insertions, 0 deletions
diff --git a/src/tests/synchroqueue.cpp b/src/tests/synchroqueue.cpp
new file mode 100644
index 0000000..980a4a3
--- /dev/null
+++ b/src/tests/synchroqueue.cpp
@@ -0,0 +1,131 @@
1#include <bu/thread.h>
2#include <bu/synchroqueue.h>
3#include <bu/list.h>
4
5class Thing
6{
7public:
8 Thing( int x ) :
9 x( x ),
10 y( 0 )
11 {
12 }
13
14 int x;
15 int y;
16};
17
18typedef Bu::SynchroQueue<Thing *> ThingQueue;
19
20Bu::Mutex mWorkDone;
21int iWorkDone;
22Bu::Condition cWorkDone;
23
24void workDone()
25{
26 mWorkDone.lock();
27 iWorkDone--;
28 if( iWorkDone == 0 )
29 {
30 mWorkDone.unlock();
31 cWorkDone.lock();
32 cWorkDone.signal();
33 cWorkDone.unlock();
34 return;
35 }
36 mWorkDone.unlock();
37}
38
39class ThingEater : public Bu::Thread
40{
41public:
42 ThingEater( ThingQueue &qThing ) :
43 qThing( qThing )
44 {
45 }
46
47 bool bRunning;
48
49 void setRunning( bool b )
50 {
51 mRunning.lock();
52 bRunning = b;
53 mRunning.unlock();
54 }
55
56 bool isRunning()
57 {
58 mRunning.lock();
59 bool b = bRunning;
60 mRunning.unlock();
61 return b;
62 }
63
64protected:
65 virtual void run()
66 {
67 setRunning( true );
68 while( isRunning() )
69 {
70 Thing *pThing = qThing.dequeue( 0, 250000 );
71 if( pThing == NULL )
72 continue;
73
74 pThing->y = pThing->x*2;
75 usleep( 10000 );
76
77 workDone();
78 }
79 }
80
81 ThingQueue &qThing;
82 Bu::Mutex mRunning;
83};
84
85typedef Bu::List<ThingEater *> ThingEaterList;
86
87int main()
88{
89 ThingQueue qThing;
90 ThingEaterList lEater;
91
92 mWorkDone.lock();
93 iWorkDone = 1000;
94 mWorkDone.unlock();
95
96 for( int j = 0; j < 5; j++ )
97 lEater.append( new ThingEater( qThing ) );
98
99 for( ThingEaterList::iterator i = lEater.begin(); i; i++ )
100 (*i)->start();
101
102 for( int j = 0; j < 1000; j++ )
103 {
104 qThing.enqueue( new Thing( j ) );
105 }
106
107 mWorkDone.lock();
108 mWorkDone.unlock();
109 cWorkDone.lock();
110 for(;;)
111 {
112 mWorkDone.lock();
113 if( iWorkDone == 0 )
114 {
115 mWorkDone.unlock();
116 break;
117 }
118 mWorkDone.unlock();
119 cWorkDone.wait();
120 }
121 cWorkDone.unlock();
122
123 for( ThingEaterList::iterator i = lEater.begin(); i; i++ )
124 (*i)->setRunning( false );
125
126 for( ThingEaterList::iterator i = lEater.begin(); i; i++ )
127 (*i)->join();
128
129 return 0;
130}
131