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.
10 #include "ace/OS_NS_unistd.h"
11 #include "ace/OS_main.h"
14 #if defined (ACE_WIN32)
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
>
25 //FUZZ: disable check_for_lack_ACE_OS
26 virtual int open (void *);
27 //FUZZ: enable check_for_lack_ACE_OS
31 // Use two handles here..
32 ACE_sema_t sema_handles_
[2];
36 static WFMO_Test wfmo_test
;
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);
53 int result
= ::WaitForMultipleObjects (2, this->sema_handles_
,
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)
66 ACE_ERROR ((LM_ERROR
, "Error in WaitForMultipleObejcts\n"));
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.
77 "(%t) thread has been signaled.\n"));
79 // Yield this thread so that the other one(s) have a chance to
88 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
90 int thread_count
= THREAD_COUNT
;
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.
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.
128 ACE_TMAIN (int, ACE_TCHAR
*[])
130 ACE_DEBUG ((LM_DEBUG
, "this test only runs on Win32\n"));
132 #endif /* ACE_WIN32 */