Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / APG / ThreadManagement / Coop_Cancel.cpp
blob37129d4466e276bf7cb6122c4a60047034da1a21
1 #include "ace/config-lite.h"
2 #if defined (ACE_HAS_THREADS)
4 #include "ace/OS_NS_time.h"
5 #include "ace/OS_NS_unistd.h"
6 #include "ace/Task.h"
7 #include "ace/Log_Msg.h"
9 // Listing 1 code/ch13
10 class CanceledTask : public ACE_Task<ACE_MT_SYNCH>
12 public:
13 virtual int svc ()
15 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) starting up\n")));
17 // Cache our ACE_Thread_Manager pointer.
18 ACE_Thread_Manager *mgr = this->thr_mgr ();
19 while (1)
21 if (mgr->testcancel (mgr->thr_self ()))
22 return 0;
24 ACE_Message_Block *mb = 0;
25 ACE_Time_Value tv (0, 1000);
26 tv += ACE_OS::time (0);
27 int result = this->getq (mb, &tv);
28 if (result == -1 && errno == EWOULDBLOCK)
29 continue;
30 else
32 // Do real work.
36 ACE_NOTREACHED (return 0);
39 // Listing 1
41 // Listing 2 code/ch13
42 int ACE_TMAIN (int, ACE_TCHAR *[])
44 CanceledTask task;
45 task.activate ();
47 ACE_OS::sleep (1);
49 ACE_Thread_Manager::instance ()->cancel_task (&task);
50 task.wait ();
51 return 0;
53 // Listing 2
55 #else
56 #include "ace/OS_main.h"
57 #include "ace/OS_NS_stdio.h"
59 int ACE_TMAIN (int, ACE_TCHAR *[])
61 ACE_OS::puts (ACE_TEXT ("This example requires threads."));
62 return 0;
65 #endif /* ACE_HAS_THREADS */