Use =default for skeleton copy constructor
[ACE_TAO.git] / ACE / examples / Reactor / Proactor / test_timeout.cpp
blob1d511a34c0682e8e5b9b407d3657d115fb785db3
2 //=============================================================================
3 /**
4 * @file test_timeout.cpp
6 * This example application shows how to write event loops that
7 * handle events for some fixed amount of time. Note that any
8 * thread in the Proactor thread pool can call back the handler. On
9 * POSIX4 systems, this test works only with POSIX_SIG_Proactor,
10 * which can work with multiple threads.
12 * @author Irfan Pyarali and Alexander Babu Arulanthu
14 //=============================================================================
16 #include "ace/Proactor.h"
17 #include "ace/Task.h"
18 #include "ace/Atomic_Op.h"
19 #include "ace/OS_NS_sys_time.h"
20 #include "ace/OS_NS_unistd.h"
21 #include "ace/OS_main.h"
23 #if defined (ACE_HAS_WIN32_OVERLAPPED_IO) || defined (ACE_HAS_AIO_CALLS)
24 // This only works on Win32 platforms and on Unix platforms supporting
25 // POSIX aio calls.
27 /**
28 * @class Timeout_Handler
30 * @brief Generic timeout handler.
32 class Timeout_Handler : public ACE_Handler
34 public:
35 Timeout_Handler ()
36 : start_time_ (ACE_OS::gettimeofday ())
40 virtual void handle_time_out (const ACE_Time_Value &tv,
41 const void *arg)
43 // Print out when timeouts occur.
44 ACE_DEBUG ((LM_DEBUG, "(%t) %d timeout occurred for %s @ %d.\n",
45 ++count_,
46 (char *) arg,
47 (tv - this->start_time_).sec ()));
49 // Sleep for a while
50 ACE_OS::sleep (4);
53 private:
54 /// Number of the timer event.
55 ACE_Atomic_Op <ACE_SYNCH_MUTEX, int> count_;
57 /// Starting time of the test.
58 ACE_Time_Value start_time_;
61 class Worker : public ACE_Task <ACE_NULL_SYNCH>
63 public:
64 int svc ()
66 // Handle events for 13 seconds.
67 ACE_Time_Value run_time (13);
69 ACE_DEBUG ((LM_DEBUG, "(%t):Starting svc routine\n"));
71 if (ACE_Proactor::run_event_loop(run_time) == -1)
72 ACE_ERROR_RETURN ((LM_ERROR, "(%t):%p.\n", "Worker::svc"), -1);
74 ACE_DEBUG ((LM_DEBUG, "(%t) work complete\n"));
76 return 0;
80 int
81 ACE_TMAIN (int, ACE_TCHAR *[])
83 Timeout_Handler handler;
85 // Register a 2 second timer.
86 ACE_Time_Value foo_tv (2);
87 if (ACE_Proactor::instance ()->schedule_timer (handler,
88 (void *) "Foo",
89 ACE_Time_Value::zero,
90 foo_tv) == -1)
91 ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "schedule_timer"), -1);
93 // Register a 3 second timer.
94 ACE_Time_Value bar_tv (3);
95 if (ACE_Proactor::instance ()->schedule_timer (handler,
96 (void *) "Bar",
97 ACE_Time_Value::zero,
98 bar_tv) == -1)
99 ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "schedule_timer"), -1);
101 Worker worker;
103 if (worker.activate (THR_NEW_LWP, 10) == -1)
104 ACE_ERROR_RETURN ((LM_ERROR, "%p.\n", "main"), -1);
106 ACE_Thread_Manager::instance ()->wait ();
108 return 0;
111 #else /* ACE_HAS_WIN32_OVERLAPPED_IO || ACE_HAS_AIO_CALLS */
114 ACE_TMAIN (int, ACE_TCHAR *[])
116 ACE_DEBUG ((LM_DEBUG,
117 "This example is multithreaded version of test_timeout_st.cpp\n"
118 "This doesnt work on this platform !!!\n"));
119 return 1;
122 #endif /* ACE_HAS_WIN32_OVERLAPPED_IO || ACE_HAS_AIO_CALLS */