diff options
Diffstat (limited to 'src/tests/synchroqueue.cpp')
-rw-r--r-- | src/tests/synchroqueue.cpp | 198 |
1 files changed, 99 insertions, 99 deletions
diff --git a/src/tests/synchroqueue.cpp b/src/tests/synchroqueue.cpp index 980a4a3..9e8c787 100644 --- a/src/tests/synchroqueue.cpp +++ b/src/tests/synchroqueue.cpp | |||
@@ -5,14 +5,14 @@ | |||
5 | class Thing | 5 | class Thing |
6 | { | 6 | { |
7 | public: | 7 | public: |
8 | Thing( int x ) : | 8 | Thing( int x ) : |
9 | x( x ), | 9 | x( x ), |
10 | y( 0 ) | 10 | y( 0 ) |
11 | { | 11 | { |
12 | } | 12 | } |
13 | 13 | ||
14 | int x; | 14 | int x; |
15 | int y; | 15 | int y; |
16 | }; | 16 | }; |
17 | 17 | ||
18 | typedef Bu::SynchroQueue<Thing *> ThingQueue; | 18 | typedef Bu::SynchroQueue<Thing *> ThingQueue; |
@@ -23,109 +23,109 @@ Bu::Condition cWorkDone; | |||
23 | 23 | ||
24 | void workDone() | 24 | void workDone() |
25 | { | 25 | { |
26 | mWorkDone.lock(); | 26 | mWorkDone.lock(); |
27 | iWorkDone--; | 27 | iWorkDone--; |
28 | if( iWorkDone == 0 ) | 28 | if( iWorkDone == 0 ) |
29 | { | 29 | { |
30 | mWorkDone.unlock(); | 30 | mWorkDone.unlock(); |
31 | cWorkDone.lock(); | 31 | cWorkDone.lock(); |
32 | cWorkDone.signal(); | 32 | cWorkDone.signal(); |
33 | cWorkDone.unlock(); | 33 | cWorkDone.unlock(); |
34 | return; | 34 | return; |
35 | } | 35 | } |
36 | mWorkDone.unlock(); | 36 | mWorkDone.unlock(); |
37 | } | 37 | } |
38 | 38 | ||
39 | class ThingEater : public Bu::Thread | 39 | class ThingEater : public Bu::Thread |
40 | { | 40 | { |
41 | public: | 41 | public: |
42 | ThingEater( ThingQueue &qThing ) : | 42 | ThingEater( ThingQueue &qThing ) : |
43 | qThing( qThing ) | 43 | qThing( qThing ) |
44 | { | 44 | { |
45 | } | 45 | } |
46 | 46 | ||
47 | bool bRunning; | 47 | bool bRunning; |
48 | 48 | ||
49 | void setRunning( bool b ) | 49 | void setRunning( bool b ) |
50 | { | 50 | { |
51 | mRunning.lock(); | 51 | mRunning.lock(); |
52 | bRunning = b; | 52 | bRunning = b; |
53 | mRunning.unlock(); | 53 | mRunning.unlock(); |
54 | } | 54 | } |
55 | 55 | ||
56 | bool isRunning() | 56 | bool isRunning() |
57 | { | 57 | { |
58 | mRunning.lock(); | 58 | mRunning.lock(); |
59 | bool b = bRunning; | 59 | bool b = bRunning; |
60 | mRunning.unlock(); | 60 | mRunning.unlock(); |
61 | return b; | 61 | return b; |
62 | } | 62 | } |
63 | 63 | ||
64 | protected: | 64 | protected: |
65 | virtual void run() | 65 | virtual void run() |
66 | { | 66 | { |
67 | setRunning( true ); | 67 | setRunning( true ); |
68 | while( isRunning() ) | 68 | while( isRunning() ) |
69 | { | 69 | { |
70 | Thing *pThing = qThing.dequeue( 0, 250000 ); | 70 | Thing *pThing = qThing.dequeue( 0, 250000 ); |
71 | if( pThing == NULL ) | 71 | if( pThing == NULL ) |
72 | continue; | 72 | continue; |
73 | 73 | ||
74 | pThing->y = pThing->x*2; | 74 | pThing->y = pThing->x*2; |
75 | usleep( 10000 ); | 75 | usleep( 10000 ); |
76 | 76 | ||
77 | workDone(); | 77 | workDone(); |
78 | } | 78 | } |
79 | } | 79 | } |
80 | 80 | ||
81 | ThingQueue &qThing; | 81 | ThingQueue &qThing; |
82 | Bu::Mutex mRunning; | 82 | Bu::Mutex mRunning; |
83 | }; | 83 | }; |
84 | 84 | ||
85 | typedef Bu::List<ThingEater *> ThingEaterList; | 85 | typedef Bu::List<ThingEater *> ThingEaterList; |
86 | 86 | ||
87 | int main() | 87 | int main() |
88 | { | 88 | { |
89 | ThingQueue qThing; | 89 | ThingQueue qThing; |
90 | ThingEaterList lEater; | 90 | ThingEaterList lEater; |
91 | 91 | ||
92 | mWorkDone.lock(); | 92 | mWorkDone.lock(); |
93 | iWorkDone = 1000; | 93 | iWorkDone = 1000; |
94 | mWorkDone.unlock(); | 94 | mWorkDone.unlock(); |
95 | 95 | ||
96 | for( int j = 0; j < 5; j++ ) | 96 | for( int j = 0; j < 5; j++ ) |
97 | lEater.append( new ThingEater( qThing ) ); | 97 | lEater.append( new ThingEater( qThing ) ); |
98 | 98 | ||
99 | for( ThingEaterList::iterator i = lEater.begin(); i; i++ ) | 99 | for( ThingEaterList::iterator i = lEater.begin(); i; i++ ) |
100 | (*i)->start(); | 100 | (*i)->start(); |
101 | 101 | ||
102 | for( int j = 0; j < 1000; j++ ) | 102 | for( int j = 0; j < 1000; j++ ) |
103 | { | 103 | { |
104 | qThing.enqueue( new Thing( j ) ); | 104 | qThing.enqueue( new Thing( j ) ); |
105 | } | 105 | } |
106 | 106 | ||
107 | mWorkDone.lock(); | 107 | mWorkDone.lock(); |
108 | mWorkDone.unlock(); | 108 | mWorkDone.unlock(); |
109 | cWorkDone.lock(); | 109 | cWorkDone.lock(); |
110 | for(;;) | 110 | for(;;) |
111 | { | 111 | { |
112 | mWorkDone.lock(); | 112 | mWorkDone.lock(); |
113 | if( iWorkDone == 0 ) | 113 | if( iWorkDone == 0 ) |
114 | { | 114 | { |
115 | mWorkDone.unlock(); | 115 | mWorkDone.unlock(); |
116 | break; | 116 | break; |
117 | } | 117 | } |
118 | mWorkDone.unlock(); | 118 | mWorkDone.unlock(); |
119 | cWorkDone.wait(); | 119 | cWorkDone.wait(); |
120 | } | 120 | } |
121 | cWorkDone.unlock(); | 121 | cWorkDone.unlock(); |
122 | 122 | ||
123 | for( ThingEaterList::iterator i = lEater.begin(); i; i++ ) | 123 | for( ThingEaterList::iterator i = lEater.begin(); i; i++ ) |
124 | (*i)->setRunning( false ); | 124 | (*i)->setRunning( false ); |
125 | 125 | ||
126 | for( ThingEaterList::iterator i = lEater.begin(); i; i++ ) | 126 | for( ThingEaterList::iterator i = lEater.begin(); i; i++ ) |
127 | (*i)->join(); | 127 | (*i)->join(); |
128 | 128 | ||
129 | return 0; | 129 | return 0; |
130 | } | 130 | } |
131 | 131 | ||