fork(1) download
  1.  
  2. class CMutexRW
  3. {
  4. protected:
  5. HANDLE m_semReaders;
  6. HANDLE m_semWriters;
  7. int m_nReaders;
  8. public:
  9. CMutexRW() :
  10. m_semReaders(NULL),
  11. m_semWriters(NULL),
  12. m_nReaders(0)
  13. {
  14. // initialize the Readers & Writers variables
  15. m_semReaders = ::CreateSemaphore(NULL, 1, 1, NULL);
  16. m_semWriters = ::CreateSemaphore(NULL, 1, 1, NULL);
  17. m_nReaders = 0;
  18.  
  19. if (m_semReaders == NULL || m_semWriters == NULL)
  20. {
  21. LPVOID lpMsgBuf;
  22. FormatMessage(
  23. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  24. FORMAT_MESSAGE_FROM_SYSTEM |
  25. FORMAT_MESSAGE_IGNORE_INSERTS,
  26. NULL,
  27. GetLastError(),
  28. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  29. (LPTSTR) &lpMsgBuf,
  30. 0,
  31. NULL
  32. );
  33. TRACE( "***** ERROR: CreateSemaphore: %s\n", (LPCTSTR)lpMsgBuf );
  34. LocalFree( lpMsgBuf );
  35. }
  36. };
  37.  
  38. virtual ~CMutexRW()
  39. {
  40. if (m_semWriters)
  41. VERIFY( ::CloseHandle(m_semWriters) );
  42.  
  43. m_semWriters = NULL;
  44. if (m_semReaders)
  45. VERIFY( ::CloseHandle(m_semReaders) );
  46. m_semReaders = NULL;
  47. }
  48.  
  49. inline void Lock_DataRead(){
  50. DWORD dwEvent = WAIT_TIMEOUT;
  51.  
  52. // P( semReaders )
  53. dwEvent = ::WaitForSingleObject( m_semReaders, INFINITE );
  54. ASSERT(dwEvent == WAIT_OBJECT_0);
  55.  
  56. m_nReaders++;
  57.  
  58. if (m_nReaders == 1)
  59. {
  60. // P( semWriters )
  61. dwEvent = ::WaitForSingleObject( m_semWriters, INFINITE );
  62. ASSERT(dwEvent == WAIT_OBJECT_0);
  63. }
  64. // V( semReaders )
  65. VERIFY( ::ReleaseSemaphore( m_semReaders, 1, NULL ) );
  66. };
  67.  
  68. inline void Unlock_DataRead(){
  69. DWORD dwEvent = WAIT_TIMEOUT;
  70. // P( semReaders )
  71. dwEvent = ::WaitForSingleObject( m_semReaders, INFINITE );
  72. ASSERT(dwEvent == WAIT_OBJECT_0);
  73.  
  74. m_nReaders--;
  75.  
  76. if (m_nReaders == 0)
  77. {
  78. // V( semWriters )
  79. VERIFY( ::ReleaseSemaphore(m_semWriters, 1, NULL) );
  80. }
  81. // V( semReaders )
  82. VERIFY( ::ReleaseSemaphore( m_semReaders, 1, NULL ) );
  83. };
  84.  
  85. inline void Lock_DataWrite(){
  86. DWORD dwEvent = WAIT_TIMEOUT;
  87.  
  88. // P( semWriters )
  89. dwEvent = ::WaitForSingleObject( m_semWriters, INFINITE );
  90. ASSERT(dwEvent == WAIT_OBJECT_0);
  91. }
  92.  
  93. inline void Unlock_DataWrite(){
  94. // V( semWriters )
  95. VERIFY( ::ReleaseSemaphore(m_semWriters, 1, NULL) );
  96. };
  97.  
  98. };
  99.  
  100.  
  101. class CReadLock
  102. {
  103. protected:
  104. CMutexRW* m_pMutexRW;
  105. bool m_bIsLocked;
  106. public:
  107. CReadLock(CMutexRW* pMutexRW, const bool bInitialLock = false) :
  108. m_pMutexRW(pMutexRW), m_bIsLocked(false)
  109. {
  110. ASSERT(m_pMutexRW);
  111. if (bInitialLock){
  112. m_pMutexRW->Lock_DataRead();
  113. m_bIsLocked = true;
  114. }
  115. };
  116.  
  117. inline const bool& IsLocked() const{
  118. return m_bIsLocked;
  119. };
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:5:5: error: unknown type name 'HANDLE'
    HANDLE        m_semReaders;
    ^
prog.cpp:6:5: error: unknown type name 'HANDLE'
    HANDLE        m_semWriters;
    ^
prog.cpp:10:22: error: use of undeclared identifier 'NULL'
        m_semReaders(NULL),
                     ^
prog.cpp:11:22: error: use of undeclared identifier 'NULL'
        m_semWriters(NULL),
                     ^
prog.cpp:15:29: error: no member named 'CreateSemaphore' in the global namespace
        m_semReaders    = ::CreateSemaphore(NULL, 1, 1, NULL);
                          ~~^
prog.cpp:15:45: error: use of undeclared identifier 'NULL'
        m_semReaders    = ::CreateSemaphore(NULL, 1, 1, NULL);
                                            ^
prog.cpp:15:57: error: use of undeclared identifier 'NULL'
        m_semReaders    = ::CreateSemaphore(NULL, 1, 1, NULL);
                                                        ^
prog.cpp:16:29: error: no member named 'CreateSemaphore' in the global namespace
        m_semWriters    = ::CreateSemaphore(NULL, 1, 1, NULL);
                          ~~^
prog.cpp:16:45: error: use of undeclared identifier 'NULL'
        m_semWriters    = ::CreateSemaphore(NULL, 1, 1, NULL);
                                            ^
prog.cpp:16:57: error: use of undeclared identifier 'NULL'
        m_semWriters    = ::CreateSemaphore(NULL, 1, 1, NULL);
                                                        ^
prog.cpp:19:29: error: use of undeclared identifier 'NULL'
        if (m_semReaders == NULL || m_semWriters == NULL)
                            ^
prog.cpp:19:53: error: use of undeclared identifier 'NULL'
        if (m_semReaders == NULL || m_semWriters == NULL)
                                                    ^
prog.cpp:21:13: error: unknown type name 'LPVOID'
            LPVOID lpMsgBuf;
            ^
prog.cpp:27:17: error: use of undeclared identifier 'GetLastError'
                GetLastError(),
                ^
prog.cpp:29:18: error: use of undeclared identifier 'LPTSTR'
                (LPTSTR) &lpMsgBuf,
                 ^
prog.cpp:23:17: error: use of undeclared identifier 'FORMAT_MESSAGE_ALLOCATE_BUFFER'
                FORMAT_MESSAGE_ALLOCATE_BUFFER | 
                ^
prog.cpp:24:17: error: use of undeclared identifier 'FORMAT_MESSAGE_FROM_SYSTEM'
                FORMAT_MESSAGE_FROM_SYSTEM | 
                ^
prog.cpp:25:17: error: use of undeclared identifier 'FORMAT_MESSAGE_IGNORE_INSERTS'
                FORMAT_MESSAGE_IGNORE_INSERTS,
                ^
prog.cpp:26:17: error: use of undeclared identifier 'NULL'
                NULL,
                ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
stdout
Standard output is empty