File: Tokens_Deadlock.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 (69 lines) | stat: -rw-r--r-- 1,562 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
// $Id: Tokens_Deadlock.cpp 80826 2008-03-04 14:51:23Z wotte $

#include "ace/Local_Tokens.h"
#include "ace/Task.h"
#include "ace/OS_NS_unistd.h"

#if defined (ACE_HAS_TOKENS_LIBRARY)

// Listing 1 code/ch14
class ThreadOne : public ACE_Task_Base
{
public:
  virtual int svc (void)
  {
    ACE_Local_Mutex mutex1 (ACE_TEXT ("resource1"),
                            0, // Deadlock detection enabled.
                            1);// Debugging enabled.
    mutex1.acquire ();
    ACE_OS::sleep (2);
    ACE_Local_Mutex mutex2 (ACE_TEXT ("resource2"), 0, 1);
    mutex2.acquire ();
    return 0;
  }
};

class ThreadTwo : public ACE_Task_Base
{
public:
  virtual int svc (void)
  {
    ACE_Local_Mutex mutex2 (ACE_TEXT ("resource2"),
                            0, // Deadlock detection enabled.
                            1);// Debugging enabled.
    mutex2.acquire ();
    ACE_OS::sleep (2);
    ACE_Local_Mutex mutex1 (ACE_TEXT ("resource1"),
                            0, // Deadlock detection enabled.
                            1);// Debugging enabled.
    mutex1.acquire ();
    return 0;
  }
};

int ACE_TMAIN (int, ACE_TCHAR *[])
{
  ThreadOne t1;
  ThreadTwo t2;

  t1.activate ();
  ACE_OS::sleep (1);
  t2.activate ();
  t1.wait ();
  t2.wait ();

  return 0;
}
// Listing 1

#else /* defined (ACE_HAS_TOKENS_LIBRARY) */

int ACE_TMAIN (int, ACE_TCHAR *[])
{
  ACE_ERROR ((LM_ERROR,
              ACE_TEXT ("local tokens not supported ")
              ACE_TEXT ("on this platform\n")));
  return 0;
}

#endif /* defined (ACE_HAS_TOKENS_LIBRARY) */