From fc7fc244600f504b6f832d8106a9218ef6a9b88b Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 22 Feb 2013 00:15:43 +0000 Subject: The test for Bu::ReadWriteMutex is better and there is more documentation now. --- src/tests/readwritemutex.cpp | 59 ++++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 16 deletions(-) (limited to 'src/tests/readwritemutex.cpp') diff --git a/src/tests/readwritemutex.cpp b/src/tests/readwritemutex.cpp index 3ed8a49..8cb79e5 100644 --- a/src/tests/readwritemutex.cpp +++ b/src/tests/readwritemutex.cpp @@ -11,15 +11,32 @@ using namespace Bu; +#define CNT 15 + +char disp[CNT*2+1]; + ReadWriteMutex mRW; bool bRunning; +int rbase; + +Mutex mDisp; + +void dispUpd( char &ind, char val ) +{ + mDisp.lock(); + ind = val; + println(disp); + mDisp.unlock(); +} + class Writer : public Thread { public: - Writer( int iId ) : + Writer( int iId, char &ind ) : iId( iId ), - rand( iId ) + rand( rbase+iId ), + ind( ind ) { } @@ -30,12 +47,15 @@ public: protected: virtual void run() { + usleep( rand.rand(1000000) ); while( bRunning ) { mRW.lockWrite(); - println("Writer %1 locking.").arg( iId ); - usleep( rand.rand(5,10)*100000 ); - println("Writer %1 unlocking.").arg( iId ); + dispUpd( ind, 'W' ); +// println("Writer %1 locking.").arg( iId ); + usleep( rand.rand(1,10)*10000 ); +// println("Writer %1 unlocking.").arg( iId ); + dispUpd( ind, ' ' ); mRW.unlockWrite(); usleep( rand.rand(5,10)*10000 ); } @@ -44,14 +64,16 @@ protected: private: int iId; RandomCmwc rand; + char &ind; }; class Reader : public Thread { public: - Reader( int iId ) : + Reader( int iId, char &ind ) : iId( iId ), - rand( -iId ) + rand( rbase-iId ), + ind( ind ) { } @@ -62,12 +84,15 @@ public: protected: virtual void run() { + usleep( rand.rand(1000000) ); while( bRunning ) { mRW.lockRead(); - println("Reader %1 locking.").arg( iId ); - usleep( rand.rand(5,10)*100000 ); - println("Reader %1 unlocking.").arg( iId ); + dispUpd( ind, 'R' ); +// println("Reader %1 locking.").arg( iId ); + usleep( rand.rand(1,10)*10000 ); +// println("Reader %1 unlocking.").arg( iId ); + dispUpd( ind, ' ' ); mRW.unlockRead(); usleep( rand.rand(5,10)*10000 ); } @@ -76,26 +101,28 @@ protected: private: int iId; RandomCmwc rand; + char &ind; }; -#define CNT 5 - int main() { bRunning = true; + rbase = time( NULL ); + + memset( disp, 0x20, CNT*2+1 ); + disp[CNT*2] = 0; Thread **threads = new Thread*[CNT*2]; for( int j = 0; j < CNT; j++ ) { - threads[j] = new Reader( j+1 ); - threads[j+CNT] = new Writer( j+1 ); + threads[j] = new Reader( j+1, disp[j+CNT] ); + threads[j+CNT] = new Writer( j+1, disp[j] ); } - println("Starting."); for( int j = 0; j < CNT*2; j++ ) threads[j]->start(); - sleep( 10 ); + sleep( 120 ); bRunning = false; for( int j = 0; j < CNT*2; j++ ) -- cgit v1.2.3