您的位置:首页 > 其它

游戏服务器之跨平台线程的实现

2014-01-06 11:31 218 查看
一个跨windows和linux平台的线程基类的实现。

需要实现的在头文件的接口如下

实现的功能就是一些线程的基本功能(部分功能需要的话还需要拓展,在子类实现):

例程、挂起、恢复、终止、线程句柄、线程id.

不同平台也会有些差异,主要是使用上,对于线程id、互斥量、条件变量的使用。只有windows有线程句柄,windows的线程事件通知和等待是通过线程句柄,linux使用条件变量。线程间同步都可以使用互斥量。

现在已经有人做了类似的功能使windows和linux下使用相同的方法和类来实现线程的跨平台,连名字都是相同的,以后会分析下那个第三方库。

               

#ifdef WINDOWS
/* 线程基础类
*/
class CBaseThread
{
private:
HANDLE
m_hThread; //线程句柄
DWORD m_dwThreadId;//线程ID
bool m_boTerminated;//线程终止标记,子类在线程例程循环中应当通过调用terminated函数判断线程是否被设置了终止标记
private:
HANDLE CreateThreadHandle(const DWORD dwFlags);
static void CALLBACK ThreadRoutine(CBaseThread *lpThread);
protected:
inline bool blocking(){return true;}
//terminated函数用于判断现成是否被设置了终止标记
inline bool terminated(){ return m_boTerminated; }
//线程事物处理例程函数,子类应当继承此函数
virtual void OnRountine();
//线程被终止后的通知函数,nSuspendCount参数表示线程被终止的次数
virtual void OnSuspend(int nSuspendCount);
//线程被恢复后的通知函数,nSuspendCount参数表示线程还需恢复多少次后才能恢复执行,为0表示线程已恢复执行
virtual void OnResume(int nSuspendCount);
//线程例程终止后的通知函数
virtual void OnTerminated();
public:
CBaseThread();
CBaseThread(bool boCreateSuspended);
~CBaseThread();

//获取线程句柄
inline HANDLE getHandle(){ return m_hThread; }
//获取线程ID
inline DWORD getThreadId(){ return m_dwThreadId; }
//暂停线程的执行,返回值表示线程到本次暂停操作后为止总计被暂停的次数。若返回-1则表示暂停线程失败。
int suspend();
//恢复线程的执行,返回值表示线程到本次恢复操作后,线程仍需恢复多少次才能恢复执行。
//若返回-1则表示恢复线程失败,返回0表示线程已经完全恢复执行。
int resume();
//获取线程优先级,失败会返回THREAD_PRIORITY_ERROR_RETURN,否则返回值表示线程优先级
int getPriority();
//设置线程优先级,成功则返回true
bool setPriority(int nPriority);
//等待线程执行完毕,dwWaitLong参数表示等待的最大毫秒数,INFINITE表示无限等待。
//注意,调用此函数的线程在此线程执行完毕后会一直处于阻塞状态
//参数boWaitAlertAble表示调用线程在阻塞期间是否允许进入警告状态(仅对于windows有效)
int waitFor(DWORD dwWaitLong = INFINITE, bool boWaitAlertAble = true);
//标记线程的终止标记
void terminate();
};
#else//linux
class CBaseThread
{
public:
friend void *threadRoutine(CBaseThread *lpThread);
CBaseThread();
CBaseThread(bool boCreateSuspended);
virtual ~CBaseThread();
int suspend();
int resume();
int getPriority();
bool setPriority(int nPriority);
void terminate();
///暂时无限等待
int waitFor();
protected:
inline bool blocking()
{
if(m_isBlock)
{
pthread_mutex_lock(&m_blockMutex);
pthread_cond_wait(&m_blockCond, &m_blockMutex);
}
return true;
}
inline bool terminated(){ return m_isBlock; }
virtual void OnRountine(){};
virtual void OnSuspend(int nSuspendCount){};
virtual void OnResume(int nSuspendCount){};
virtual void OnTerminated(){};
private:
void createThread();
static void *threadRoutine(CBaseThread *lpThread);
private:
pthread_t m_pid;
pthread_mutex_t m_blockMutex;
pthread_cond_t m_blockCond;
bool m_isBlock;
};

#endif

源文件中的代码实现:

WINDOWS是自定义的windows下的宏

#ifdef WINDOWS

CBaseThread::CBaseThread()

{
m_hThread = NULL;
m_dwThreadId = 0;
m_boTerminated = false;
CreateThreadHandle( 0 );

}

CBaseThread::CBaseThread(bool boCreateSuspended)

{
m_hThread = NULL;
m_dwThreadId = 0;
m_boTerminated = false;
CreateThreadHandle( boCreateSuspended ? CREATE_SUSPENDED : 0 );

}

CBaseThread::~CBaseThread()

{
terminate();
waitFor();
if ( m_hThread )
{
CloseHandle( m_hThread );
m_hThread = NULL;
}

}

HANDLE CBaseThread::CreateThreadHandle(const DWORD dwFlags)

{
m_hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)ThreadRoutine, this, dwFlags, &m_dwThreadId);
return m_hThread;

}

void CALLBACK CBaseThread::ThreadRoutine(CBaseThread *lpThread)

{
lpThread->OnRountine();
lpThread->OnTerminated();
ExitThread(0);

}

void CBaseThread::OnRountine()

{

}

void CBaseThread::OnSuspend(int nSuspendCount)

{

}

void CBaseThread::OnResume(int nSuspendCount)

{

}

void CBaseThread::OnTerminated()

{

}

int CBaseThread::suspend()

{
DWORD dwSuspended;
dwSuspended = SuspendThread( m_hThread );
if ( dwSuspended != -1 )
{
OnSuspend(dwSuspended + 1);
return dwSuspended + 1;
}
return dwSuspended;

}

int CBaseThread::resume()

{
DWORD dwSuspended;
dwSuspended = ResumeThread( m_hThread );
if ( dwSuspended != -1 )
{
OnResume(dwSuspended - 1);
return dwSuspended - 1;
}
return dwSuspended;

}

int CBaseThread::getPriority()

{
return GetThreadPriority( m_hThread );

}

bool CBaseThread::setPriority(int nPriority)

{
return SetThreadPriority( m_hThread, nPriority ) != 0;

}

int CBaseThread::waitFor(DWORD dwWaitLong, bool boWaitAlertAble)

{
return WaitForSingleObjectEx( m_hThread, dwWaitLong, boWaitAlertAble );

}

void CBaseThread::terminate()

{
m_boTerminated = true;

}

#else

namespace lib

{
namespace thread
{

CBaseThread::CBaseThread()
{
m_pid = 0;
m_isBlock = false;
pthread_mutex_init(&m_blockMutex, 0);
pthread_cond_init(&m_blockCond, 0);
createThread();
}
CBaseThread::CBaseThread(bool boCreateSuspended)
{
m_pid = 0;
m_isBlock = true;
pthread_mutex_init(&m_blockMutex, 0);
pthread_cond_init(&m_blockCond, 0);
createThread();
}
CBaseThread::~CBaseThread()
{
pthread_mutex_destroy(&m_blockMutex);
pthread_cond_destroy(&m_blockCond);
}

int CBaseThread::suspend()
{
m_isBlock = true;
///暂时返回
return 0;
}

int CBaseThread::resume()
{
m_isBlock = false;
pthread_cond_signal(&m_blockCond);
///暂时返回
return 0;
}

int CBaseThread::getPriority()
{
int policy;
sched_param sp;
if(0 != pthread_getschedparam(m_pid, &policy, &sp))
{
return 0;
}
return sp.sched_priority;
}

bool CBaseThread::setPriority(int nPriority)
{
sched_param sp = {nPriority};
if(0 == pthread_setschedparam(m_pid, SCHED_RR, &sp))
{
return true;
}
return false;
}

void CBaseThread::terminate()
{
pthread_cancel(m_pid);
}

int CBaseThread::waitFor()
{
pthread_join(m_pid, 0);
return 0;
}

void CBaseThread::createThread()
{
pthread_create(&m_pid, 0, (LPTHREAD_START_ROUTINE)(&CBaseThread::threadRoutine), this);
}

void *CBaseThread::threadRoutine(CBaseThread *lpThread)
{
lpThread->OnRountine();
lpThread->OnTerminated();
pthread_exit(0);
}
}

}

#endif
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: