Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / APG / ThreadManagement / Signals2.cpp
blob138abd0112702d8d725ee44afef20889e1a96755
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"
8 #include "ace/Signal.h"
9 #include "ace/Sig_Handler.h"
11 class SignalableTask : public ACE_Task<ACE_MT_SYNCH>
13 public:
14 virtual int handle_signal (int signum, siginfo_t * = 0, ucontext_t * = 0)
16 if (signum == SIGUSR1)
18 ACE_DEBUG ((LM_DEBUG,
19 ACE_TEXT ("(%t) received a %S signal\n"),
20 signum));
21 handle_alert();
24 return 0;
27 virtual int svc ()
29 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) Starting thread\n")));
31 while (1)
33 ACE_Message_Block* mb = 0;
34 ACE_Time_Value tv (0, 1000);
35 tv += ACE_OS::time (0);
37 int result = this->getq(mb, &tv);
39 if (result == -1 && errno == EWOULDBLOCK)
40 continue;
41 else
42 process_message (mb);
45 ACE_NOTREACHED (return 0);
48 void handle_alert ();
49 void process_message (ACE_Message_Block *mb);
52 void
53 SignalableTask::process_message (ACE_Message_Block *)
55 return;
58 void
59 SignalableTask::handle_alert ()
61 return;
64 // Listing 1 code/ch13
65 int ACE_TMAIN (int, ACE_TCHAR *[])
67 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) Main thread\n")));
68 SignalableTask handler;
69 handler.activate (THR_NEW_LWP | THR_JOINABLE, 5);
71 ACE_Sig_Handler sh;
72 sh.register_handler (SIGUSR1, &handler);
74 ACE_OS::sleep (1); // Allow threads to start
76 for (int i = 0; i < 5; i++)
77 ACE_OS::kill (ACE_OS::getpid (), SIGUSR1);
78 handler.wait ();
79 return 0;
81 // Listing 1
83 #else
84 #include "ace/OS_main.h"
85 #include "ace/OS_NS_stdio.h"
87 int ACE_TMAIN (int, ACE_TCHAR *[])
89 ACE_OS::puts (ACE_TEXT ("This example requires threads."));
90 return 0;
93 #endif /* ACE_HAS_THREADS */