2 //=============================================================================
4 * @file test_multiple_loops.cpp
6 * This example application shows how to write programs that
7 * combine the Proactor and Reactor event loops. This is possible
8 * only on WIN32 platform.
10 * @author Irfan Pyarali
12 //=============================================================================
16 #include "ace/Proactor.h"
17 #include "ace/WIN32_Proactor.h"
18 #include "ace/Atomic_Op.h"
19 #include "ace/OS_NS_unistd.h"
22 #if defined (ACE_HAS_WIN32_OVERLAPPED_IO)
25 * @class Timeout_Handler
27 * @brief Generic timeout handler.
29 class Timeout_Handler
: public ACE_Handler
, public ACE_Event_Handler
36 // This is called by the Proactor. This is declared in ACE_Handler.
37 virtual void handle_time_out (const ACE_Time_Value
&tv
,
40 // Print out when timeouts occur.
41 ACE_DEBUG ((LM_DEBUG
, "(%t) %d timeout occurred for %s @ %d.\n",
46 // Since there is only one thread that can do the timeouts in
47 // Reactor, lets keep the handle_timeout short for that
49 if (ACE_OS::strcmp ((char *) arg
, "Proactor") == 0)
54 // This method is declared in ACE_Event_Handler.
55 virtual int handle_timeout (const ACE_Time_Value
&tv
,
58 this->handle_time_out (tv
, arg
);
63 ACE_Atomic_Op
<ACE_Thread_Mutex
, int> count_
;
66 class Worker
: public ACE_Task
<ACE_NULL_SYNCH
>
72 ACE_DEBUG ((LM_DEBUG
, "(%t) Worker started\n"));
74 // Handle events for 13 seconds.
75 ACE_Time_Value
run_time (13);
77 // Try to become the owner
78 ACE_Reactor::instance ()->owner (ACE_Thread::self ());
80 if (ACE_Reactor::run_event_loop (run_time
) == -1)
81 ACE_ERROR_RETURN ((LM_ERROR
, "%p.\n", "Worker::svc"), -1);
83 ACE_DEBUG ((LM_DEBUG
, "(%t) work complete\n"));
90 ACE_TMAIN (int, ACE_TCHAR
*[])
92 Timeout_Handler handler
;
93 ACE_WIN32_Proactor
win32_proactor (0, 1);
94 ACE_Proactor
proactor (&win32_proactor
, 0, 0);
96 ACE_Reactor::instance ()->register_handler (proactor
.implementation ());
98 // Register a 2 second timer.
99 ACE_Time_Value
foo_tv (2);
100 if (proactor
.schedule_timer (handler
,
102 ACE_Time_Value::zero
,
104 ACE_ERROR_RETURN ((LM_ERROR
, "%p\n", "schedule_timer"), -1);
106 // Register a 3 second timer.
107 ACE_Time_Value
bar_tv (3);
108 if (ACE_Reactor::instance ()->schedule_timer (&handler
,
110 ACE_Time_Value::zero
,
112 ACE_ERROR_RETURN ((LM_ERROR
, "%p\n", "schedule_timer"), -1);
116 if (worker
.activate (THR_NEW_LWP
, 10) == -1)
117 ACE_ERROR_RETURN ((LM_ERROR
, "%p.\n", "main"), -1);
119 ACE_Thread_Manager::instance ()->wait ();
121 // Remove from reactor
122 ACE_Reactor::instance ()->remove_handler (&proactor
,
123 ACE_Event_Handler::DONT_CALL
);
129 ACE_TMAIN (int, ACE_TCHAR
*[])
133 #endif /* ACE_HAS_WIN32_OVERLAPPED_IO */