ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/threads.cpp
(Generate patch)
# Line 2 | Line 2
2   #include <list>
3   #include "GThreads.h"
4  
5 +
6   using namespace std;
7   //using namespace tthread;
8  
# Line 19 | Line 20
20   GConditionVar gCond;
21  
22   // Thread function: Thread ID
23 < void ThreadIDs(void * aArg, GThread* thisThread)
23 > void ThreadIDs(GThreadData& my)
24   {
25      {
26 <            GLockGuard<GMutex> lock(gMutex);
27 <            cout << " My thread id is " << thisThread->get_id() << "." << endl << flush;
26 >      GLockGuard<GMutex> lock(gMutex);
27 >      
28 >      //cout << " My thread id is " << thisThread->get_id() << "." << endl << flush;
29 >      cout << " My thread id is " << my.thread->get_id() << "." << endl << flush;
30      }
31    this_thread::sleep_for(chrono::seconds(4));
32   }
33  
34   #ifndef GTHREADS_NO_TLS
35   // Thread function: Thread-local storage
36 < void ThreadTLS(void * aArg, GThread*)
36 > void ThreadTLS(void * aArg)
37   {
38    gLocalVar = 2;
39    cout << " My gLocalVar is " << gLocalVar << "." << endl;
# Line 38 | Line 41
41   #endif
42  
43   // Thread function: Mutex locking
44 < void ThreadLock(void * aArg, GThread*)
44 > void ThreadLock(void * aArg)
45   {
46    for(int i = 0; i < 10000; ++ i)
47    {
# Line 48 | Line 51
51   }
52  
53   // Thread function: Mutex locking
54 < void ThreadLock2(void * aArg, GThread*)
54 > void ThreadLock2(void * aArg)
55   {
56    for(int i = 0; i < 10000; ++ i)
57    {
# Line 58 | Line 61
61   }
62  
63   // Thread function: Condition notifier
64 < void ThreadCondition1(void * aArg, GThread*)
64 > void ThreadCondition1(void * aArg)
65   {
66    GLockGuard<GMutex> lock(gMutex);
67    -- gCount;
# Line 66 | Line 69
69   }
70  
71   // Thread function: Condition waiter
72 < void ThreadCondition2(void * aArg, GThread*)
72 > void ThreadCondition2(void * aArg)
73   {
74 <  cout << " Wating..." << flush;
74 >  cout << " Wating:" << flush;
75    GLockGuard<GMutex> lock(gMutex);
76    while(gCount > 0)
77    {
78 <    cout << "." << flush;
78 >    cout << "*" << gCount << flush;
79      gCond.wait(gMutex);
80 +    cout << "received notification at " << gCount << endl << flush;
81    }
82 <  cout << "." << endl;
82 >  cout << "-reached 0!" << endl;
83   }
84  
85   // Thread function: Yield
86 < void ThreadYield(void * aArg, GThread*)
86 > void ThreadYield(void * aArg)
87   {
88    // Yield...
89    this_thread::yield();
# Line 94 | Line 98
98    cout << " Number of processor cores: " << GThread::hardware_concurrency() << endl;
99  
100    // Test 2: thread IDs
101 +  /*
102    cout << endl << "PART II: Thread IDs" << endl;
103    {
104      // Show the main thread ID
# Line 191 | Line 196
196      // Check the global count
197      cout << " gCount = " << gCount << endl;
198    }
199 <
199 > */
200    // Test 6: condition variable
201    cout << endl << "PART VI: Condition variable (40 + 1 threads)" << endl;
202    {
203      // Set the global counter to the number of threads to run.
204 <    gCount = 40;
204 >    {
205 >     GLockGuard<GMutex> lock(gMutex);
206 >     gCount = 6;
207 >    }
208  
209      // Start the waiting thread (it will wait for gCount to reach zero).
210      GThread t1(ThreadCondition2, 0);
211 +    
212 +    this_thread::sleep_for(chrono::milliseconds(400));
213 +      {
214 +      
215 +       for (int c=5;c>=0;c--) {
216 +           {
217 +           GLockGuard<GMutex> lock(gMutex);
218 +           --gCount;
219 +           gCond.notify_one();
220 +           }
221 +           cout << "Notify_" << c << "|" << flush;
222 +           //this_thread::sleep_for(chrono::milliseconds(200));
223 +           cout << "Notify_sent" << c << "|" << flush;
224 +           }
225 +      }
226  
227      // Start a bunch of child threads (these will decrease gCount by 1 when they
228      // finish)
229 +    /*
230      list<GThread *> threadList;
231      for(int i = 0; i < 40; ++ i)
232        threadList.push_back(new GThread(ThreadCondition1, 0));
233 <
233 >    */
234      // Wait for the waiting thread to finish
235      t1.join();
236  
237      // Wait for the other threads to finish
238 +    /*
239      list<GThread *>::iterator it;
240      for(it = threadList.begin(); it != threadList.end(); ++ it)
241      {
# Line 218 | Line 243
243        t->join();
244        delete t;
245      }
246 +    */
247    }
248 <
248 > /*
249    // Test 7: yield
250    cout << endl << "PART VII: Yield (40 + 1 threads)" << endl;
251    {
# Line 253 | Line 279
279      }
280      cout << endl;
281    }
282 +  */
283   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines