Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / APG / ThreadManagement / Priorities.cpp
blob4f2c477f1a3bd4909d74da87294c0e3f0ed82d29
1 #include "ace/config-lite.h"
3 #if defined (ACE_HAS_THREADS)
5 #include "ace/Task.h"
6 #include "ace/Log_Msg.h"
7 #include "ace/OS_NS_unistd.h"
9 // Listing 2 code/ch13
10 class HA_CommandHandler : public ACE_Task<ACE_MT_SYNCH>
12 public:
13 HA_CommandHandler (const char *name) : name_ (name)
14 { }
16 virtual int svc ()
18 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) starting up %C\n"),
19 name_));
21 ACE_OS::sleep (2);
22 ACE_Message_Block *mb = 0;
23 while (this->getq (mb) != -1)
25 if (mb->msg_type () == ACE_Message_Block::MB_BREAK)
27 mb->release ();
28 break;
30 process_message (mb);
31 mb->release ();
33 return 0;
36 void process_message (ACE_Message_Block *)
38 ACE_DEBUG ((LM_DEBUG,
39 ACE_TEXT ("(%t) Processing message %C\n"),
40 name_));
41 // Simulate compute bound task.
42 for (int i = 0; i < 100; i++)
46 private:
47 const char *name_;
49 // Listing 2
51 #if !defined (ACE_THR_PRI_OTHER_MAX)
52 // This should be fixed in ACE... There's no _MAX, _MIN values for
53 // thread priorities.
54 #if defined (ACE_WIN32)
55 # define ACE_THR_PRI_OTHER_MAX ((ACE_THR_PRI_OTHER_DEF) + 1)
56 #elif defined (VXWORKS)
57 # define ACE_THR_PRI_OTHER_MAX 0
58 #endif
59 #endif
61 // Listing 1 code/ch13
62 int ACE_TMAIN (int, ACE_TCHAR *[])
64 HA_CommandHandler hp_handler ("HighPriority");
65 hp_handler.activate (THR_NEW_LWP | THR_JOINABLE,
66 1, 1, ACE_THR_PRI_OTHER_MAX);
68 HA_CommandHandler lp_handler ("LowPriority");
69 lp_handler.activate (THR_NEW_LWP | THR_JOINABLE,
70 1, 1, ACE_THR_PRI_OTHER_DEF);
72 ACE_Message_Block mb;
73 for (int i = 0; i < 100; i++)
75 ACE_Message_Block *mb_hp, *mb_lp;
76 mb_hp = mb.clone ();
77 mb_lp = mb.clone ();
78 hp_handler.putq (mb_hp);
79 lp_handler.putq (mb_lp);
82 ACE_Message_Block stop (0, ACE_Message_Block::MB_BREAK);
83 hp_handler.putq (stop.clone ());
84 lp_handler.putq (stop.clone ());
85 hp_handler.wait ();
86 lp_handler.wait ();
88 return 0;
90 // Listing 1
92 #else
93 #include "ace/OS_main.h"
94 #include "ace/OS_NS_stdio.h"
96 int ACE_TMAIN (int, ACE_TCHAR *[])
98 ACE_OS::puts (ACE_TEXT ("This example requires threads."));
99 return 0;
102 #endif /* ACE_HAS_THREADS */