// $Id$ // This test program illustrates that the Win32 // function can be called in multiple // threads, all of which wait on the same set of HANDLEs. Note that // the dispatching of the threads should be relatively "fair" (i.e., // everyone gets a chance to process the various HANDLEs as they // become active). Thanks to Ari Erev for // suggesting this and providing the initial code. #include "ace/Task.h" #if defined (ACE_WIN32) // Number of threads. static const int THREAD_COUNT = 5; // Number of iterations. static const int MAX_ITERATIONS = 100; class WFMO_Test : public ACE_Task { public: virtual int open (void *); virtual int svc (void); // Use two handles here.. ACE_sema_t sema_handles_[2]; int semaphore_count_; }; static WFMO_Test wfmo_test; int WFMO_Test::open (void *arg) { int thread_count = int (arg); ACE_ASSERT (this->activate (0, thread_count) != -1); return 0; } int WFMO_Test::svc (void) { while(1) { int result = ::WaitForMultipleObjects (2, this->sema_handles_, FALSE, INFINITE); if (result == WAIT_OBJECT_0) // Signal the other semaphore just to see if we can get // another thread to wakeup. ACE_ASSERT (ACE_OS::sema_post (&sema_handles_[1]) != -1); else if (result == WAIT_OBJECT_0 + 1) ; else { ACE_DEBUG ((LM_DEBUG, "Error in WaitForMultipleObejcts\n")); ACE_OS::exit (0); } // semaphore_count_ will be displayed by the "main" thread. It's // value must be 2. Note that although this is a shared // resource it's not protected via a mutex because the ++ // operation on Intel is atomic. semaphore_count_++; ACE_DEBUG ((LM_DEBUG, "(%t) thread has been signaled.\n")); // Yield this thread so that the other one(s) have a chance to // run. ACE_OS::thr_yield (); } return 0; } int main (int argc, char *argv[]) { int thread_count = THREAD_COUNT; if (argc > 1) thread_count = ACE_OS::atoi (argv[1]); wfmo_test.open ((void *) thread_count); // Initialize the semaphores. ACE_ASSERT (ACE_OS::sema_init (&wfmo_test.sema_handles_[0], thread_count + 5) != -1); ACE_ASSERT (ACE_OS::sema_init (&wfmo_test.sema_handles_[1], thread_count + 5) != -1); for (int i = 0; i < MAX_ITERATIONS; i++) { wfmo_test.semaphore_count_ = 0; ACE_ASSERT (ACE_OS::sema_post (&wfmo_test.sema_handles_[0]) != -1); // No real synchronization here. Just sleep enough so that at // least one (or two threads) run as a result of the semaphore. ACE_OS::sleep (1); // Add one for the other thread that was signaled. ACE_DEBUG ((LM_DEBUG, "semaphore_count_ = %d (should have been %d).\n", wfmo_test.semaphore_count_, 2)); // Two semaphores should have been released. } ACE_OS::exit (0); return 0; } #else int main (int, char *[]) { ACE_DEBUG ((LM_DEBUG, "this test only runs on Win32\n")); } #endif /* ACE_WIN32 */