blob: 032dd159dd1a74a02fc279b04f7990f358caf19c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
#include "ace/config-lite.h"
#if defined (ACE_HAS_THREADS)
#include "ace/Synch.h"
#include "ace/Task.h"
// Listing 1 code/ch12
class HA_Device_Repository
{
public:
HA_Device_Repository ()
{ }
void update_device (int device_id)
{
mutex_.acquire ();
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) Updating device %d\n"),
device_id));
ACE_OS::sleep (1);
mutex_.release ();
}
private:
ACE_Thread_Mutex mutex_;
};
// Listing 1
// Listing 2 code/ch12
class HA_CommandHandler : public ACE_Task_Base
{
public:
enum {NUM_USES = 10};
HA_CommandHandler (HA_Device_Repository& rep) : rep_(rep)
{ }
virtual int svc (void)
{
ACE_DEBUG
((LM_DEBUG, ACE_TEXT ("(%t) Handler Thread running\n")));
for (int i=0; i < NUM_USES; i++)
this->rep_.update_device (i);
return 0;
}
private:
HA_Device_Repository & rep_;
};
int ACE_TMAIN (int, ACE_TCHAR *[])
{
HA_Device_Repository rep;
HA_CommandHandler handler1 (rep);
HA_CommandHandler handler2 (rep);
handler1.activate ();
handler2.activate ();
handler1.wait ();
handler2.wait ();
return 0;
}
// Listing 2
#else
#include "ace/OS_main.h"
#include "ace/OS_NS_stdio.h"
int ACE_TMAIN (int, ACE_TCHAR *[])
{
ACE_OS::puts (ACE_TEXT ("This example requires threads."));
return 0;
}
#endif /* ACE_HAS_THREADS */
|