#ifndef THREAD_MUTEX_HPP_
#define THREAD_MUTEX_HPP_
// #include <standard library headers>
// #include <other library headers>
#ifdef _WIN32
#include <Windows.h>
WINBASEAPI BOOL WINAPI
TryEnterCriticalSection(LPCRITICAL_SECTION lpCriticalSection);
#else
#include <pthread.h>
#endif
// #include "customer headers"
namespace thread
{
#ifdef _WIN32
class Mutex
{
public:
Mutex() { ::InitializeCriticalSection(&cs); }
~Mutex() { ::DeleteCriticalSection(&cs); }
private:
Mutex(const Mutex& rhs);
Mutex& operator=(const Mutex& rhs);
public:
void Lock() { ::EnterCriticalSection(&cs); }
void UnLock() { ::LeaveCriticalSection(&cs); }
bool TryLock() { return (::TryEnterCriticalSection(&cs) == TRUE); }
private:
//volatile HANDLE mMutex;
CRITICAL_SECTION cs;
};
#else
class Mutex
{
public:
Mutex() { pthread_mutex_init(&mutex, NULL); }
~Mutex() { pthread_mutex_destroy(&mutex); }
private:
Mutex(const Mutex& rhs);
Mutex& operator=(const Mutex& rhs);
public:
void Lock() { pthread_mutex_lock(&mutex); }
void UnLock() { pthread_mutex_unlock(&mutex); }
bool TryLock() { return (pthread_mutex_trylock(&mutex) == 0); }
private:
pthread_mutex_t mutex;
};
#endif // _WIN32
线程类封装(5)
最新推荐文章于 2017-12-15 15:38:18 发布