File: auto_event.cpp

package info (click to toggle)
ace 6.2.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 49,348 kB
  • ctags: 42,082
  • sloc: cpp: 342,284; perl: 32,718; ansic: 20,838; sh: 3,759; python: 828; exp: 787; yacc: 511; xml: 330; lex: 158; lisp: 116; makefile: 82; csh: 20; tcl: 5
file content (115 lines) | stat: -rw-r--r-- 2,956 bytes parent folder | download
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// $Id: auto_event.cpp 97383 2013-10-23 08:44:20Z mhengstmengel $

// This test shows the use of an ACE_Auto_Event as a signaling
// mechanism. Two threads are created (one a reader, the other a
// writer). The reader waits till the writer has completed
// calculations. Upon waking up the reader prints the data calculated
// by the writer. The writer thread calculates the value and signals
// the reader when the calculation completes.

#include "ace/OS_NS_unistd.h"
#include "ace/OS_main.h"
#include "ace/Service_Config.h"
#include "ace/Auto_Event.h"
#include "ace/Singleton.h"
#include "ace/Thread_Manager.h"

#if defined (ACE_HAS_THREADS)
// Shared event between reader and writer.  The ACE_Thread_Mutex is
// necessary to make sure that only one ACE_Auto_Event is created.
// The default constructor for ACE_Auto_Event sets it initially into
// the non-signaled state.

typedef ACE_Singleton <ACE_Auto_Event, ACE_Thread_Mutex> EVENT;

// work time for writer
static int work_time;

// Reader thread.
static void *
reader (void *arg)
{
  // Shared data via a reference.
  int& data = *(int *) arg;

  // Wait for writer to complete.

  ACE_DEBUG ((LM_DEBUG, "(%t) reader: waiting......\n"));

  if (EVENT::instance ()->wait () == -1)
    {
      ACE_ERROR ((LM_ERROR, "thread wait failed"));
      ACE_OS::exit (0);
    }

  // Read shared data.
  ACE_DEBUG ((LM_DEBUG, "(%t) reader: value of data is: %d\n", data));

  return 0;
}

// Writer thread.
static void *
writer (void *arg)
{
  int& data = *(int *) arg;

  // Calculate (work).
  ACE_DEBUG ((LM_DEBUG, "(%t) writer: working for %d secs\n", work_time));
  ACE_OS::sleep (work_time);

  // Write shared data.
  data = 42;

  // Wake up reader.
  ACE_DEBUG ((LM_DEBUG, "(%t) writer: calculation complete, waking reader\n"));

  if (EVENT::instance ()->signal () == -1)
    {
      ACE_ERROR ((LM_ERROR, "thread signal failed"));
      ACE_OS::exit (0);
    }

  return 0;
}

int
ACE_TMAIN (int argc, ACE_TCHAR **argv)
{
  // Shared data: set by writer, read by reader.
  int data;

  // Work time for writer.
  work_time = argc == 2 ? ACE_OS::atoi (argv[1]) : 5;

  // threads manager
  ACE_Thread_Manager& tm = *ACE_Thread_Manager::instance ();

  // Create reader thread.
  if (tm.spawn ((ACE_THR_FUNC) reader, (void *) &data) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "thread create for reader failed"), -1);

  // Create writer thread.
  if (tm.spawn ((ACE_THR_FUNC) writer, (void *) &data) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "thread create for writer failed"), -1);

  // Wait for both.
  if (tm.wait () == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "thread wait failed"), -1);
  else
    ACE_DEBUG ((LM_ERROR, "graceful exit\n"));

  return 0;
}

ACE_SINGLETON_TEMPLATE_INSTANTIATE(ACE_Singleton, ACE_Auto_Event, ACE_Thread_Mutex);


#else
int
ACE_TMAIN (int, ACE_TCHAR *[])
{
  ACE_ERROR ((LM_ERROR, "threads not supported on this platform\n"));
  return 0;
}
#endif /* ACE_HAS_THREADS */