Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / APG / ThreadSafety / Mutex.cpp
blob937ec61b6b10bc66849fe208c25aac03dc514b56
1 #include "ace/config-lite.h"
2 #if defined (ACE_HAS_THREADS)
4 #include "ace/Synch.h"
5 #include "ace/Guard_T.h"
7 class LogMessage
9 public:
10 enum { CRITICAL, NORMAL};
12 virtual ~LogMessage ()
16 virtual int priority ()
18 return NORMAL;
22 class CriticalLogMessage : public LogMessage
24 virtual int priority ()
26 return LogMessage::CRITICAL;
30 // Listing 1 code/ch14
31 typedef ACE_Thread_Mutex MUTEX;
32 class Logger
34 public:
35 void log (LogMessage *msg)
37 ACE_GUARD (MUTEX, mon, mutex_);
38 if (msg->priority () == LogMessage::CRITICAL)
39 logCritical (msg);
42 void logCritical (LogMessage *)
44 // Acquires the same mutex as log()!
45 ACE_GUARD(MUTEX, mon, mutex_);
48 private:
49 MUTEX mutex_;
52 static Logger logger;
54 int ACE_TMAIN (int, ACE_TCHAR *[])
56 CriticalLogMessage cm;
57 logger.log(&cm); // Will cause deadlock.
58 return 0;
60 // Listing 1
62 #else
63 #include "ace/OS_main.h"
64 #include "ace/OS_NS_stdio.h"
66 int ACE_TMAIN (int, ACE_TCHAR *[])
68 ACE_OS::puts (ACE_TEXT ("This example requires threads."));
69 return 0;
72 #endif /* ACE_HAS_THREADS */