aboutsummaryrefslogtreecommitdiff
path: root/src/tests
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/tests/readwritemutex.cpp59
1 files changed, 43 insertions, 16 deletions
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 @@
11 11
12using namespace Bu; 12using namespace Bu;
13 13
14#define CNT 15
15
16char disp[CNT*2+1];
17
14ReadWriteMutex mRW; 18ReadWriteMutex mRW;
15bool bRunning; 19bool bRunning;
16 20
21int rbase;
22
23Mutex mDisp;
24
25void dispUpd( char &ind, char val )
26{
27 mDisp.lock();
28 ind = val;
29 println(disp);
30 mDisp.unlock();
31}
32
17class Writer : public Thread 33class Writer : public Thread
18{ 34{
19public: 35public:
20 Writer( int iId ) : 36 Writer( int iId, char &ind ) :
21 iId( iId ), 37 iId( iId ),
22 rand( iId ) 38 rand( rbase+iId ),
39 ind( ind )
23 { 40 {
24 } 41 }
25 42
@@ -30,12 +47,15 @@ public:
30protected: 47protected:
31 virtual void run() 48 virtual void run()
32 { 49 {
50 usleep( rand.rand(1000000) );
33 while( bRunning ) 51 while( bRunning )
34 { 52 {
35 mRW.lockWrite(); 53 mRW.lockWrite();
36 println("Writer %1 locking.").arg( iId ); 54 dispUpd( ind, 'W' );
37 usleep( rand.rand(5,10)*100000 ); 55// println("Writer %1 locking.").arg( iId );
38 println("Writer %1 unlocking.").arg( iId ); 56 usleep( rand.rand(1,10)*10000 );
57// println("Writer %1 unlocking.").arg( iId );
58 dispUpd( ind, ' ' );
39 mRW.unlockWrite(); 59 mRW.unlockWrite();
40 usleep( rand.rand(5,10)*10000 ); 60 usleep( rand.rand(5,10)*10000 );
41 } 61 }
@@ -44,14 +64,16 @@ protected:
44private: 64private:
45 int iId; 65 int iId;
46 RandomCmwc rand; 66 RandomCmwc rand;
67 char &ind;
47}; 68};
48 69
49class Reader : public Thread 70class Reader : public Thread
50{ 71{
51public: 72public:
52 Reader( int iId ) : 73 Reader( int iId, char &ind ) :
53 iId( iId ), 74 iId( iId ),
54 rand( -iId ) 75 rand( rbase-iId ),
76 ind( ind )
55 { 77 {
56 } 78 }
57 79
@@ -62,12 +84,15 @@ public:
62protected: 84protected:
63 virtual void run() 85 virtual void run()
64 { 86 {
87 usleep( rand.rand(1000000) );
65 while( bRunning ) 88 while( bRunning )
66 { 89 {
67 mRW.lockRead(); 90 mRW.lockRead();
68 println("Reader %1 locking.").arg( iId ); 91 dispUpd( ind, 'R' );
69 usleep( rand.rand(5,10)*100000 ); 92// println("Reader %1 locking.").arg( iId );
70 println("Reader %1 unlocking.").arg( iId ); 93 usleep( rand.rand(1,10)*10000 );
94// println("Reader %1 unlocking.").arg( iId );
95 dispUpd( ind, ' ' );
71 mRW.unlockRead(); 96 mRW.unlockRead();
72 usleep( rand.rand(5,10)*10000 ); 97 usleep( rand.rand(5,10)*10000 );
73 } 98 }
@@ -76,26 +101,28 @@ protected:
76private: 101private:
77 int iId; 102 int iId;
78 RandomCmwc rand; 103 RandomCmwc rand;
104 char &ind;
79}; 105};
80 106
81#define CNT 5
82
83int main() 107int main()
84{ 108{
85 bRunning = true; 109 bRunning = true;
110 rbase = time( NULL );
111
112 memset( disp, 0x20, CNT*2+1 );
113 disp[CNT*2] = 0;
86 114
87 Thread **threads = new Thread*[CNT*2]; 115 Thread **threads = new Thread*[CNT*2];
88 for( int j = 0; j < CNT; j++ ) 116 for( int j = 0; j < CNT; j++ )
89 { 117 {
90 threads[j] = new Reader( j+1 ); 118 threads[j] = new Reader( j+1, disp[j+CNT] );
91 threads[j+CNT] = new Writer( j+1 ); 119 threads[j+CNT] = new Writer( j+1, disp[j] );
92 } 120 }
93 121
94 println("Starting.");
95 for( int j = 0; j < CNT*2; j++ ) 122 for( int j = 0; j < CNT*2; j++ )
96 threads[j]->start(); 123 threads[j]->start();
97 124
98 sleep( 10 ); 125 sleep( 120 );
99 bRunning = false; 126 bRunning = false;
100 127
101 for( int j = 0; j < CNT*2; j++ ) 128 for( int j = 0; j < CNT*2; j++ )