Skip to content

Instantly share code, notes, and snippets.

@cynthia
Last active August 29, 2015 13:57
Show Gist options
  • Save cynthia/9683222 to your computer and use it in GitHub Desktop.
Save cynthia/9683222 to your computer and use it in GitHub Desktop.
// POSIX pthread compatibility shim for Windows.
#ifdef _WIN32
#include <Windows.h>
typedef HANDLE pthread_mutex_t;
static int pthread_mutex_init(pthread_mutex_t *mutex, void *unused)
{
unused = NULL;
*mutex = CreateMutex(NULL, FALSE, NULL);
return *mutex == NULL ? -1 : 0;
}
static int pthread_mutex_destroy(pthread_mutex_t *mutex)
{
return CloseHandle(*mutex) == 0 ? -1 : 0;
}
static int pthread_mutex_lock(pthread_mutex_t *mutex)
{
return WaitForSingleObject(*mutex, INFINITE) == WAIT_OBJECT_0? 0 : -1;
}
static int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
return ReleaseMutex(*mutex) == 0 ? -1 : 0;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment