Skip to content

Instantly share code, notes, and snippets.

@LiamKarlMitchell
Created January 23, 2016 07:46
Show Gist options
  • Save LiamKarlMitchell/f57b1a814a77f1cc4eb4 to your computer and use it in GitHub Desktop.
Save LiamKarlMitchell/f57b1a814a77f1cc4eb4 to your computer and use it in GitHub Desktop.
// Another thing I made ages ago feel free to use :)
// licensed under MIT / Public / do whatever.
#ifndef __MUTEX_H
#define __MUTEX_H
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>
// To make this cross platform I need to read this
// http://www.comptechdoc.org/os/linux/programming/c/linux_pgcmutex.html
// Required for cross platform
// Windows
typedef HANDLE MutexHandle;
// Linux
//typedef pthread_mutex_t MutexHandle;
// Mac
class Mutex
{
private:
MutexHandle* h;
bool Released;
public:
Mutex(MutexHandle* _h)
{
h=_h;
if (*h==NULL)
{
*h=CreateMutex(NULL,TRUE,NULL);
Released=false;
}
else
{
Aquire();
}
}
Mutex(MutexHandle* _h,LPCWSTR lpName)
{
h=_h;
if (*h==NULL)
{
*h=CreateMutexW(NULL,TRUE,lpName);
Released=false;
}
else
{
Aquire();
}
}
Mutex(MutexHandle* _h,LPCSTR lpName)
{
h=_h;
if (*h==NULL)
{
*h=CreateMutexA(NULL,TRUE,lpName);
Released=false;
}
else
{
Aquire();
}
}
~Mutex()
{
Release();
}
void Release()
{
if (!Released)
{
ReleaseMutex(*h);
Released=true;
}
}
void Aquire()
{
WaitForSingleObject(*h,INFINITE);
Released=false;
}
};
#endif // __MUTEX_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment