From 364ded646ad4de2065cdf0a2f4282c7eaf7148e6 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 18 Jul 2012 06:45:17 +0000 Subject: Bu::SynchroQueue had a bug, it seems to be all fixed now. --- src/tests/synchroqueue.cpp | 131 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 src/tests/synchroqueue.cpp (limited to 'src/tests/synchroqueue.cpp') 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 @@ +#include +#include +#include + +class Thing +{ +public: + Thing( int x ) : + x( x ), + y( 0 ) + { + } + + int x; + int y; +}; + +typedef Bu::SynchroQueue ThingQueue; + +Bu::Mutex mWorkDone; +int iWorkDone; +Bu::Condition cWorkDone; + +void workDone() +{ + mWorkDone.lock(); + iWorkDone--; + if( iWorkDone == 0 ) + { + mWorkDone.unlock(); + cWorkDone.lock(); + cWorkDone.signal(); + cWorkDone.unlock(); + return; + } + mWorkDone.unlock(); +} + +class ThingEater : public Bu::Thread +{ +public: + ThingEater( ThingQueue &qThing ) : + qThing( qThing ) + { + } + + bool bRunning; + + void setRunning( bool b ) + { + mRunning.lock(); + bRunning = b; + mRunning.unlock(); + } + + bool isRunning() + { + mRunning.lock(); + bool b = bRunning; + mRunning.unlock(); + return b; + } + +protected: + virtual void run() + { + setRunning( true ); + while( isRunning() ) + { + Thing *pThing = qThing.dequeue( 0, 250000 ); + if( pThing == NULL ) + continue; + + pThing->y = pThing->x*2; + usleep( 10000 ); + + workDone(); + } + } + + ThingQueue &qThing; + Bu::Mutex mRunning; +}; + +typedef Bu::List ThingEaterList; + +int main() +{ + ThingQueue qThing; + ThingEaterList lEater; + + mWorkDone.lock(); + iWorkDone = 1000; + mWorkDone.unlock(); + + for( int j = 0; j < 5; j++ ) + lEater.append( new ThingEater( qThing ) ); + + for( ThingEaterList::iterator i = lEater.begin(); i; i++ ) + (*i)->start(); + + for( int j = 0; j < 1000; j++ ) + { + qThing.enqueue( new Thing( j ) ); + } + + mWorkDone.lock(); + mWorkDone.unlock(); + cWorkDone.lock(); + for(;;) + { + mWorkDone.lock(); + if( iWorkDone == 0 ) + { + mWorkDone.unlock(); + break; + } + mWorkDone.unlock(); + cWorkDone.wait(); + } + cWorkDone.unlock(); + + for( ThingEaterList::iterator i = lEater.begin(); i; i++ ) + (*i)->setRunning( false ); + + for( ThingEaterList::iterator i = lEater.begin(); i; i++ ) + (*i)->join(); + + return 0; +} + -- cgit v1.2.3