#ifdef WIN32
#include "windows.h"

typedef  HANDLE  ThreadHandle;
#define ThreadFuncReturnType unsigned __stdcall
typedef   void *  ThreadFuncArgType;
#define ThreadFunc( funcName, argName ) unsigned __stdcall funcName( void* argName )
#define createThread( func, args, ptid ) (((*ptid) = (HANDLE)_beginthreadex( NULL, 0, func, args, 0, NULL )) != 0)
#define destroyThread( threadHandle ) CloseHandle( threadHandle )
#define waitThread( threadHandle ) (WaitForSingleObject(threadHandle, INFINITE) == WAIT_OBJECT_0)

typedef  CRITICAL_SECTION  CSMutex;
#define EnterCS( CSMutex ) EnterCriticalSection( &CSMutex )
#define LeaveCS( CSMutex ) LeaveCriticalSection( &CSMutex )
#define InitCS( CSMutex ) InitializeCriticalSection( &CSMutex )
#define DestroyCS( CSMutex ) DeleteCriticalSection( &CSMutex )

#else
#include <pthread.h>
#include <unistd.h>

typedef  pthread_t  ThreadHandle;
typedef   void  *  ThreadFuncReturnType;
typedef   void  *  ThreadFuncArgType;
#define ThreadFunc( funcName, argName ) void *funcName( void *argName )
#define createThread( func, args, ptid ) (pthread_create( ptid, NULL, func, args ) == 0)
#define destroyThread( threadHandle ) pthread_detach( threadHandle )
#define waitThread( threadHandle ) (pthread_join( threadHandle, NULL ) == 0)
#define Sleep( n ) sleep( n==0?1:n )

typedef  pthread_mutex_t *CSMutex;
#define InitCS( CSMutex ) do { \
               CSMutex = (pthread_mutex_t *)malloc( sizeof(pthread_mutex_t) ); \
               pthread_mutex_init( CSMutex, NULL ); \
               } while (0)

#define EnterCS( CSMutex ) pthread_mutex_lock( CSMutex )
#define LeaveCS( CSMutex ) pthread_mutex_unlock( CSMutex )
#define DestroyCS( CSMutex ) do { \
               pthread_mutex_destroy( CSMutex ); \
               free( CSMutex ); \
               } while (0)

#endif