I found this code somewhere several years ago:
#if defined(LINUX)
//Headers
#include <pthread.h>
//Data types
#define MUTEX pthread_mutex_t
#ifndef EBUSY
#define EBUSY 16 // resource busy
#endif
//Macroes
#define MUTEX_INIT(mutex) (0 == pthread_mutex_init (&(mutex), NULL))
#define MUTEX_DESTROY(mutex) (0 == pthread_mutex_destroy(&(mutex)))
#define MUTEX_LOCK(mutex) (0 == pthread_mutex_lock (&(mutex)))
#define MUTEX_UNLOCK(mutex) (0 == pthread_mutex_unlock (&(mutex)))
#define MUTEX_TRYLOCK(mutex) (EBUSY == pthread_mutex_trylock (&(mutex)))
#elif defined(WINDOWS)
//Headers
#include <windows.h>
#include <process.h>
//Data types
#define MUTEX HANDLE
//Macroes
#define MUTEX_INIT(mutex) (NULL != ((mutex) = CreateMutex (0, FALSE, 0)))
#define MUTEX_DESTROY(mutex) (0 != CloseHandle((mutex)))
#define MUTEX_LOCK(mutex) (WAIT_FAILED == WaitForSingleObject ((mutex), INFINITE))
#define MUTEX_UNLOCK(mutex) (0 == ReleaseMutex ((mutex)))
#define MUTEX_TRYLOCK(mutex) (WAIT_TIMEOUT == WaitForSingleObject((mutex), IGNORE))
#else
//Data types
#define MUTEX Please_define_either_WINDOWS_or_LINUX_in_sourcecode
#endif
These macros provide a simple way to write cross platform code that runs in a multithreaded environment.