Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / APG / ThreadManagement / Async_Cancel.cpp
blobb00697cd7547712ea0b315599c8f1701453e382d
1 #include "ace/OS_NS_unistd.h"
2 #include "ace/Task.h"
3 #include "ace/Log_Msg.h"
5 #if defined (ACE_HAS_PTHREADS) && !defined (ACE_LACKS_PTHREAD_CANCEL)
6 // Only works on Pthreads...
8 // Listing 1 code/ch13
9 class CanceledTask : public ACE_Task<ACE_MT_SYNCH>
11 public:
12 virtual int svc ()
14 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) Starting thread\n")));
16 if (this->set_cancel_mode () < 0)
17 return -1;
19 while (1)
21 // Put this thread in a compute loop.. no
22 // cancellation points are available.
26 int set_cancel_mode ()
28 cancel_state new_state;
30 // Set the cancel state to asynchronous and enabled.
31 new_state.cancelstate = PTHREAD_CANCEL_ENABLE;
32 new_state.canceltype = PTHREAD_CANCEL_ASYNCHRONOUS;
33 if (ACE_Thread::setcancelstate (new_state, 0) == -1)
34 ACE_ERROR_RETURN ((LM_ERROR,
35 ACE_TEXT ("%p\n"),
36 ACE_TEXT ("cancelstate")), -1);
37 return 0;
40 // Listing 1
41 // Listing 2 code/ch13
42 int ACE_TMAIN (int, ACE_TCHAR *[])
44 CanceledTask task;
45 task.activate ();
46 ACE_OS::sleep (1);
47 ACE_Thread_Manager::instance ()->cancel_task (&task, 1);
48 task.wait ();
50 return 0;
52 // Listing 2
54 #else /* ACE_HAS_PTHREADS */
55 int ACE_TMAIN (int, ACE_TCHAR *[])
57 ACE_OS::puts ("This example works on Pthreads platforms.\n");
58 return 0;
60 #endif /* ACE_HAS_PTHREADS */