summaryrefslogtreecommitdiff
path: root/examples/Threads/test_process_semaphore.cpp
blob: d7933897f8e765089fbb9bd4a4d7317fc587eb9e (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
// $Id$

// This program tests ACE_Process_Semaphore.  To run it, open 3 or 4
// windows and run this program in each window...

#include "ace/Synch.h"
#include "ace/Signal.h"

static sig_atomic_t done;

extern "C" void
handler (int)
{
  done = 1;
}

int
main (int argc, char *argv[])
{
  char *name = argc == 1 ? "hello" : argv[1];

  ACE_Process_Semaphore pm (1, name);

  ACE_Sig_Action sa ((ACE_SignalHandler) handler, SIGINT);

  for (int i = 0; i < 100 && !done; i++)
    {
      ACE_DEBUG ((LM_DEBUG, "(%P|%t) = acquiring\n"));
      if (pm.acquire () == -1)
	ACE_DEBUG ((LM_DEBUG, "(%P|%t) = %p\n", "acquire failed"));
      else
	ACE_DEBUG ((LM_DEBUG, "(%P|%t) = acquired\n"));

      ACE_OS::sleep (3);

      if (pm.release () == -1)
	ACE_DEBUG ((LM_DEBUG, "(%P|%t) = %p\n", "release failed"));
      else
	ACE_DEBUG ((LM_DEBUG, "(%P|%t) = released\n"));

      if (pm.tryacquire () == -1)
	ACE_DEBUG ((LM_DEBUG, "(%P|%t) = %p\n", "tryacquire failed"));
      else
	ACE_DEBUG ((LM_DEBUG, "(%P|%t) = tryacquire\n"));

      if (pm.release () == -1)
	ACE_DEBUG ((LM_DEBUG, "(%P|%t) = %p\n", "release failed"));
      else
	ACE_DEBUG ((LM_DEBUG, "(%P|%t) = released\n"));
    }

  if (argc > 2)
    pm.remove ();
  return 0;
}