#ifndef MULTITHREAD_H
#define MULTITHREAD_H


#ifdef WIN32

// Thread functions under Windows

#include <Windows.h>

typedef void * THREAD;

inline THREAD CreateNewThread(void * (*Function)(void *), void * Parameter)
{
  return CreateThread(0, 0, LPTHREAD_START_ROUTINE(Function), Parameter, 0, 0);
}

inline int KillThread(THREAD TheThread)
{
  return (TerminateThread(TheThread, 0));
}

inline void WaitThread(THREAD TheThread)
{
  WaitForSingleObject(TheThread, INFINITE);
}


class Mutex
{
  CRITICAL_SECTION lock;

 public:
  Mutex(void) { InitializeCriticalSection(&lock); }
  void Lock(void) { EnterCriticalSection(&lock); }
  void Unlock(void) { LeaveCriticalSection(&lock); }
};

// End of the thread functions under Windows



#elif defined LINUX

// Thread functions under Linux

#include <pthread.h>

typedef pthread_t THREAD;

inline THREAD CreateNewThread(void * (*Function)(void *), void * Parameter)
{
  pthread_t TheThread;
  pthread_create(&TheThread, 0, Function, Parameter);
  return (THREAD) TheThread;
}

inline int KillThread(THREAD TheThread)
{
  return !(pthread_cancel(TheThread));
}

inline void WaitThread(THREAD TheThread)
{
  pthread_join(TheThread, 0);
}

class Mutex
{
  pthread_mutex_t lock;

 public:
  Mutex() { pthread_mutex_init(&lock, 0); }
  ~Mutex() { pthread_mutex_destroy(&lock); }
  void Lock(void) { pthread_mutex_lock(&lock); }
  void Unlock(void) { pthread_mutex_unlock(&lock); }
};

// End of the thread functions under Linux

#else

#error ERROR: OS not supported



#endif

#endif
