I’m having trouble getting this code to work properly. The code creates two semaphores set to 0, then creates N threads each waiting on the first semaphore. For each iteration, the code posts N times to the first semaphore and waits N times for the second. In each thread, it first waits for the first semaphore, then does the calculation, and finally posts to the second semaphore before repeating. At least, that’s what it’s supposed to do. Unfortunately, it never seems to run on more than 1 thread (see below). The ThreadTest constructor takes the number of threads N and run uses a stopwatch to time the test and display that result. The CPU usage chart displays the three attempts below.
[code]D:\Documents\Physics\Research\RDK\Code>root
-
*
-
W E L C O M E to R O O T *
-
*
- Version 5.30/00 27 June 2011 *
-
*
- You are welcome to visit our Web site *
-
http://root.cern.ch *
-
*
ROOT 5.30/00 (tags/v5-30-00@40062, Jun 28 2011, 11:49:57 on win32)
CINT/ROOT C/C++ Interpreter version 5.18.00, July 2, 2010
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
root [0] .L tools/ThreadTest.cpp+
root [1] ThreadTest test1(1),test2(4),test3(8);
root [2] test1->run()
12.365
root [3] test2->run()
12.37
root [4] test3->run()
12.364
root [5][/code]
Am I using these wrong? Is there a better way to do this? Is this a bug?
ThreadTest.cpp (1.62 KB)
ThreadTest.h (1014 Bytes)