📄 thread.h
字号:
#ifndef THREAD_H_
#define THREAD_H_
#if defined(WIN32)
#ifndef _MT
#define _MT
#endif
#define WIN32_LEAN_AND_MEAN
#include<windows.h>
#include <process.h>
#else
#include <pthread.h>
#include <semaphore.h>
#endif
#include "exception.h"//mylibconf.h CException
#include "noncopyable.h"
#if defined(OS_SUN)
//#define _POSIX_C_SOURCE 199506L
#include<thread.h>
#endif
namespace mylib{
#if defined(WIN32)
typedef unsigned (__stdcall *P_THREAD_START) (void *);
#define chBEGINTHREADEX(security, cbStack, pfnStartAddr, \
pvParam, fdwCreate, pdwThreadId) \
((HANDLE)_beginthreadex( \
(void *) (security), \
(unsigned) (cbStack), \
(P_THREAD_START) (pfnStartAddr), \
(void *) (pvParam), \
(unsigned) (fdwCreate), \
(unsigned *) (pdwThreadId)))
#endif
class CThCond;
class noncopyable;
class CThMutex:private noncopyable
{
friend class CThCond;
protected:
#if defined(WIN32)
CRITICAL_SECTION m_tMutex;
#else
pthread_mutex_t m_tMutex;
#endif
public:
CThMutex();
virtual ~CThMutex();
bool Lock();
bool TryLock();
bool Unlock();
};
class CThCond:private noncopyable
{
protected:
#if defined(WIN32)
HANDLE m_tCond;
#else
pthread_cond_t m_tCond;
#endif
public:
CThCond();
virtual ~CThCond();
bool Signal();
bool Broadcast();
bool Wait(CThMutex& stThMutex);
bool TimedWait(CThMutex& stThMutex, const unsigned int pstAbsTime);
};
class CThSem:private noncopyable
{
protected:
#if defined(WIN32)
HANDLE m_tSem;
#else
sem_t m_tSem;
#endif
public:
CThSem(int iValue = 0);
virtual ~CThSem();
bool Wait();
bool TryWait();
bool Post();
int GetValue();
};
class CThread:private noncopyable
{
protected:
enum
{
START=1,STOP=2,ALREADY=3,SUSPEND=4
};
#if defined(WIN32)
HANDLE m_tThread;
long m_id;
#else
pthread_attr_t m_tThreadAttr;
pthread_t m_tThread;
#endif
bool m_bDetach;
int m_status;
public:
CThread(bool bDetach = true);
virtual ~CThread();
void Start();
static void SLEEP(unsigned int nSec);
#if defined(WIN32)
static DWORD WINAPI ThreadFun(PVOID pvParam);
#else
static void* ThreadFun(void *param);
#endif
protected:
//因为在析构函数中调用所以没有虚拟特性
virtual void OnDelete();
virtual void* Run()=0;
virtual void OnEnd() {} //perhaps you will delete THIS here.
virtual void OnException(CException& e) {}
};
/*
================================================================
Single Barrier
================================================================
Only one thread waits until the last thread
arrives before waking up. 一般作为全局或静态对象使用
*/
class CThSingleBar:private noncopyable
{
public:
explicit CThSingleBar(int nthreadnum/*需要等待的线程数*/);
~CThSingleBar(){};
//the thread want to wait call the fun
int Wait();
//the thread who will end call the fun
int Post();
private:
CThMutex m_lock; /* lock for structure */
CThCond m_cv; /* waiting list control */
int m_ncount; /* Number of threads to post */
int m_nposters; /* Number of threads who posted */
int m_nwaiters; /* Number of threads waiting */
bool m_breleasing; /* Still waking up sleepers */
};
class AutoMutex
{
public:
explicit AutoMutex(CThMutex &p):m_p(p){m_p.Lock();}
~AutoMutex(){try{m_p.Unlock();}catch(...){}}
private:
CThMutex &m_p;
AutoMutex(AutoMutex& a);
AutoMutex &operator=(AutoMutex& a);
};
}//end of mylib
#endif//dfa
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -