Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / Threads / wfmo.cpp
blob08ee7eb82de7c284d59bc68d5e94d169950bc8e0
1 // This test program illustrates that the Win32
2 // <WaitForMultipleObjects> function can be called in multiple
3 // threads, all of which wait on the same set of HANDLEs. Note that
4 // the dispatching of the threads should be relatively "fair" (i.e.,
5 // everyone gets a chance to process the various HANDLEs as they
6 // become active). Thanks to Ari Erev <Ari_Erev@comverse.com> for
7 // suggesting this and providing the initial code.
9 #include "ace/Task.h"
10 #include "ace/OS_NS_unistd.h"
11 #include "ace/OS_main.h"
14 #if defined (ACE_WIN32)
16 // Number of threads.
17 static const int THREAD_COUNT = 5;
19 // Number of iterations.
20 static const int MAX_ITERATIONS = 100;
22 class WFMO_Test : public ACE_Task <ACE_NULL_SYNCH>
24 public:
25 //FUZZ: disable check_for_lack_ACE_OS
26 virtual int open (void *);
27 //FUZZ: enable check_for_lack_ACE_OS
29 virtual int svc ();
31 // Use two handles here..
32 ACE_sema_t sema_handles_[2];
33 int semaphore_count_;
36 static WFMO_Test wfmo_test;
38 int
39 WFMO_Test::open (void *arg)
41 int thread_count = int (arg);
42 int result = this->activate (0, thread_count);
44 ACE_ASSERT (result != -1);
45 return 0;
48 int
49 WFMO_Test::svc ()
51 while(1)
53 int result = ::WaitForMultipleObjects (2, this->sema_handles_,
54 FALSE, INFINITE);
55 if (result == WAIT_OBJECT_0)
57 // Signal the other semaphore just to see if we can get
58 // another thread to wakeup.
59 result = ACE_OS::sema_post (&sema_handles_[1]);
60 ACE_ASSERT (result != -1);
62 else if (result == WAIT_OBJECT_0 + 1)
64 else
66 ACE_ERROR ((LM_ERROR, "Error in WaitForMultipleObejcts\n"));
67 ACE_OS::exit (0);
70 // semaphore_count_ will be displayed by the "main" thread.
71 // It's value must be 2. Note that although this is a shared
72 // resource it's not protected via a mutex because the ++
73 // operation on Intel is atomic.
75 semaphore_count_++;
76 ACE_DEBUG ((LM_DEBUG,
77 "(%t) thread has been signaled.\n"));
79 // Yield this thread so that the other one(s) have a chance to
80 // run.
81 ACE_OS::thr_yield ();
84 return 0;
87 int
88 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
90 int thread_count = THREAD_COUNT;
92 if (argc > 1)
93 thread_count = ACE_OS::atoi (argv[1]);
95 wfmo_test.open ((void *) thread_count);
97 // Initialize the semaphores.
98 int result = ACE_OS::sema_init (&wfmo_test.sema_handles_[0], thread_count + 5);
99 ACE_ASSERT (result != -1);
101 result = ACE_OS::sema_init (&wfmo_test.sema_handles_[1], thread_count + 5);
102 ACE_ASSERT (result != -1);
104 for (int i = 0; i < MAX_ITERATIONS; i++)
106 wfmo_test.semaphore_count_ = 0;
108 result = ACE_OS::sema_post (&wfmo_test.sema_handles_[0]);
109 ACE_ASSERT (result != -1);
111 // No real synchronization here. Just sleep enough so that at
112 // least one (or two threads) run as a result of the semaphore.
113 ACE_OS::sleep (1);
115 // Add one for the other thread that was signaled.
116 ACE_DEBUG ((LM_DEBUG,
117 "semaphore_count_ = %d (should have been %d).\n",
118 wfmo_test.semaphore_count_,
119 2)); // Two semaphores should have been released.
122 ACE_OS::exit (0);
124 return 0;
126 #else
128 ACE_TMAIN (int, ACE_TCHAR *[])
130 ACE_DEBUG ((LM_DEBUG, "this test only runs on Win32\n"));
132 #endif /* ACE_WIN32 */