Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class mutex {
- protected:
- bool locked;
- HANDLE mutexHandle;
- public:
- mutex(): locked(false){
- mutexHandle = CreateMutex(NULL, FALSE, NULL);
- if(!mutexHandle)
- error("failed to retrieve mutex");
- }
- void lock(){
- switch(WaitForSingleObject(mutexHandle, 2000)){
- case WAIT_TIMEOUT:
- error("mutex did not get lock in time (2 seconds maximum)");
- case WAIT_ABANDONED:
- error("mutex owner thread quit without unlocking.");
- case WAIT_FAILED:
- error("mutex failed with error " + GetLastError());
- }
- locked = true;
- }
- void unlock(){
- locked = false;
- ReleaseMutex(mutexHandle);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement