Advertisement
WeltEnSTurm

Untitled

Sep 11th, 2012
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. class mutex {
  2.     protected:
  3.         bool locked;
  4.         HANDLE mutexHandle;
  5.     public:
  6.         mutex(): locked(false){
  7.             mutexHandle = CreateMutex(NULL, FALSE, NULL);
  8.             if(!mutexHandle)
  9.                 error("failed to retrieve mutex");
  10.         }
  11.  
  12.         void lock(){
  13.             switch(WaitForSingleObject(mutexHandle, 2000)){
  14.                 case WAIT_TIMEOUT:
  15.                     error("mutex did not get lock in time (2 seconds maximum)");
  16.                 case WAIT_ABANDONED:
  17.                     error("mutex owner thread quit without unlocking.");
  18.                 case WAIT_FAILED:
  19.                     error("mutex failed with error " + GetLastError());
  20.             }
  21.             locked = true;
  22.         }
  23.  
  24.         void unlock(){
  25.             locked = false;
  26.             ReleaseMutex(mutexHandle);
  27.         }
  28.  
  29. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement