Initial commit
This commit is contained in:
+121
@@ -0,0 +1,121 @@
|
||||
#ifndef threadH
|
||||
#define threadH
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
//#include <process.h>
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#include "typedef.h"
|
||||
#endif
|
||||
|
||||
#ifndef INFINITE
|
||||
#define INFINITE 0x7FFFFFFF
|
||||
#endif
|
||||
|
||||
//////////////////////////////
|
||||
//
|
||||
// 信号量
|
||||
//
|
||||
class CSema
|
||||
{
|
||||
protected:
|
||||
bool m_bCreate;// 用来控制是否生成信号量,缺省情况下生成。
|
||||
#ifdef _WIN32
|
||||
HANDLE m_hSema;
|
||||
/*#elif defined __MACOSX__
|
||||
sem_t* m_pSema;
|
||||
char m_sName[16];
|
||||
static int m_smNo;
|
||||
#else
|
||||
sem_t m_hSema;
|
||||
*/
|
||||
#else
|
||||
pthread_mutex_t mutex;
|
||||
pthread_cond_t condition;
|
||||
int semCount;
|
||||
#endif
|
||||
|
||||
public:
|
||||
CSema(const char *sName, unsigned int nInit, bool bCreate=true);
|
||||
~CSema();
|
||||
int ActP(DWORD dwTimeOut = INFINITE); // 等待状态
|
||||
void ActV(); // 非等待状态
|
||||
};
|
||||
|
||||
///////////////////////////////
|
||||
//
|
||||
// 互斥量
|
||||
//
|
||||
class CMutexLock
|
||||
{
|
||||
protected:
|
||||
CSema * m_psmMut;
|
||||
public:
|
||||
CMutexLock(bool bCreate=true);
|
||||
CMutexLock(CSema * pSema, DWORD dwTimeOut = INFINITE);
|
||||
~CMutexLock();
|
||||
};
|
||||
|
||||
///////////////////////////////
|
||||
//
|
||||
// 线程类
|
||||
//
|
||||
class CThread
|
||||
{
|
||||
public:
|
||||
enum EPriority {
|
||||
e_LowestPriority, // 其它所有线程都停止时运行
|
||||
e_LowPriority, // 运行频率大约为普通优先级一半
|
||||
e_NormalPriority, // 普通优先级
|
||||
e_HighPriority, // 运行频率大约为普通优先级一倍
|
||||
e_HighestPriority, // 独占CPU方式运行
|
||||
e_NumPriorities
|
||||
};
|
||||
protected:
|
||||
bool m_bSuspend;
|
||||
bool m_bExit;
|
||||
int m_nStackSize;
|
||||
unsigned int m_nThreadId;
|
||||
|
||||
void ExitThread();//线程本身要求退出
|
||||
#ifdef _WIN32
|
||||
HANDLE m_hThreadHandle;
|
||||
static unsigned int __stdcall MainFunction(void *threadPtr);
|
||||
#else
|
||||
pthread_t m_hThreadHandle;
|
||||
pthread_attr_t m_tAttr;
|
||||
bool m_bStart;
|
||||
CSema m_smSuspend;
|
||||
CSema m_smSleep;
|
||||
|
||||
static void* MainFunction(void *threadPtr);
|
||||
unsigned long CompPSOSPrio(EPriority nPrioLevel);
|
||||
#endif
|
||||
|
||||
public:
|
||||
CThread(
|
||||
unsigned stackSize, // Size of stack to use for thread.
|
||||
EPriority priorityLevel = e_NormalPriority // Initial priority of thread.
|
||||
);
|
||||
|
||||
~CThread();
|
||||
|
||||
virtual void Main()=0;
|
||||
virtual void Suspend();
|
||||
virtual void Resume();
|
||||
virtual void Terminate();
|
||||
virtual bool IsTerminated() const { return m_bExit; };
|
||||
virtual bool IsSuspended() const { return m_bSuspend; };
|
||||
virtual void SetPriority(EPriority priorityLevel);
|
||||
virtual void Sleep(DWORD dwMilliseconds);
|
||||
//HANDLE GetThreadID(){return m_hThreadHandle;};
|
||||
|
||||
#ifdef _WIN32
|
||||
HANDLE GetThreadID(){return m_hThreadHandle;};
|
||||
#else
|
||||
unsigned long GetThreadID(){return m_hThreadHandle;};
|
||||
#endif
|
||||
|
||||
};
|
||||
#endif //_THREAD_H_
|
||||
Reference in New Issue
Block a user