本人拿到关于thread的封装类的.h文件,实在不明白thread与threadsystem之间的关系.不如大家帮我看看如何完善thread.cpp.恳请高人指点迷津.贴上三个文件.zthread.h, netkernel.h 及 tcpclient.h.后两者是使用thread的地方,同时给出所有涉及的文件.我想高人一定可以通过这几个文件得出最终正确结论.谢谢!//////////////////////////////////////////////////////////////////////////////////////////////////////// zhtread.h----1//线程#ifndef ZEPHYR_THREAD_H
#define ZEPHYR_THREAD_H#include <windows.h>#include "zdebug.h"
#include "zlink.h""
#include "ZLock.h"#ifndef TBASE_API
#define TBASE_API __declspec(dllimport)
#endifstruct TBASE_API ZThreadData{
DWORD dwThreadID; // 线程ID
HANDLE hThreadHandle; // 线程句柄
HANDLE hEventExit; // 线程退出事件, 当本线程被退出,那么置 为有信号,通知threadsystem从m_threads中删除自已 ZThreadData(){
hThreadHandle = hEventExit = 0;
} // add by tommy20070929
ZThreadData(const ZThreadData& thread)
{
dwThreadID = thread.dwThreadID;
hThreadHandle = thread.hThreadHandle;
hEventExit = thread.hEventExit;
} inline bool operator == (const ZThreadData &s) const; inline bool operator != (const ZThreadData &s) const;
};inline bool ZThreadData::operator == (const ZThreadData &s) const
{
return this->dwThreadID == s.dwThreadID;
}inline bool ZThreadData::operator != (const ZThreadData &s) const 

return !((*this) == s); 
}typedef unsigned long (__stdcall *_ZTHREADFUNC)(void *);DWORD WINAPI _zthreadfunc(void *lpparam);//////////////////////////////////////////////////////////////////////
// ZThreadSystemclass TBASE_API ZThreadSystem{
private:
ZCriticalSection m_ThreadsLock;
ZLinkEx<ZThreadData,16,4> m_Threads; // 管理所有线程的数据,以防止资源不被回收 HANDLE m_EventExit; // 有某个线程要退出的事件???
HANDLE m_ICanExit; // 主线程可以退出的事件,只到所有被管理的线程都退出才能退出 ZThreadData m_MainThread; // 主线程的数据放在这里
public:
ZThreadSystem() { m_EventExit = m_ICanExit = NULL; }
~ZThreadSystem() { ZASSERT(m_Threads.GetUsed() == 0); } //释放所有资源(Cleanup())
//等待所有线程关闭
void Release(); //初始化
HRESULT Init(); //创建线程, 这个由ZThread调用,返回它的数据,同时记入m_Threads
ZThreadData *CreateThread(DWORD swStackSize,unsigned long (__stdcall *start_addr)(void *),void *srglist,DWORD dwCreationFlags); //清除线程, 清除某个线程并且, 由线程调用,线程管理系统负责清理资源
void KillThread(ZThreadData *thread); //获得当前线程数据
void GetCurThreadData( ZThreadData &thread ); ////////////////////////////////////////////////////////////////////////// HANDLE GetExitEvent() { return m_EventExit; }
private:
void GetMainThreadData() { GetCurThreadData(m_MainThread); }
};//////////////////////////////////////////////////////////////////////
// ZThreadclass TBASE_API ZThread{
friend DWORD WINAPI _zthreadfunc(void *lpparam);
protected:
ZThreadData *m_ThreadData; _ZTHREADFUNC m_Func; ZThreadSystem *m_ThreadSystem; LPVOID m_pData; // 自已 zthreaddata的指针,它指向主线程的
private:
void *m_Param;
HANDLE m_hExitEvent;
HANDLE m_hWakeUpEvent;
public:
inline ZThread();
virtual ~ZThread() { } //run
//线程主运行函数
virtual HRESULT Run(void *lpparam); //create thread, 调用线程管理者的创建线程方法
virtual HRESULT CreateThread(DWORD swStackSize,unsigned long (__stdcall *start_addr)(void *),void *srglist,DWORD dwCreationFlags); //每个线程都必须调用这个函数,且如果返回true退出线程
bool ICanExit(); //关闭线程,阻塞操作
void WaitForExitThread(); //sleep,可以唤醒;Sleep(INFINITE)相当于挂起线程
bool Sleep(DWORD time); ////////////////////////////////////////////////////////////////////////// //设置ThreadSystem
//属于初始化部分的设置,必须被调用
inline void SetThreadSystem(ZThreadSystem *threadsystem); inline ZThreadSystem *GetThreadSystem() const { return m_ThreadSystem; } //Wake Up Thread
inline void WakeUp(); //唤醒线程
inline void ResumeThread(); //挂起线程,只到线程结束、进程结束,或者被WakeUp
inline void SuspendThread(); //get handle
inline HANDLE GetHandle(); //发送关闭线程消息,即时结束
inline void ExitThread(); inline HANDLE GetThreadSystemExitEvent(); HANDLE GetExitEvent() { return m_hExitEvent; } // 设置数据
void SetData(LPVOID pData) { m_pData = pData; } // 获得数据
LPVOID GetData() { return m_pData; } bool IAmRunning() {
// return m_ThreadData; 
return true;
}protected:
//线程离开,Run()函数自动调用
//重载Run()以后需要自己在线程结束时调用
void CleanUp();
};inline ZThread::ZThread()
{
m_Param = NULL;
m_Func = NULL;
m_ThreadData = NULL;
m_hExitEvent = NULL;
m_hWakeUpEvent = NULL;
m_ThreadSystem = NULL; m_pData = NULL;
};inline void ZThread::ExitThread()
{
if(m_ThreadData)
SetEvent(m_hExitEvent);
}inline void ZThread::SetThreadSystem(ZThreadSystem *threadsystem)
{
ZASSERT(threadsystem); m_ThreadSystem = threadsystem;
}inline void ZThread::ResumeThread()
{
ZASSERT(m_hWakeUpEvent); WakeUp();
}inline void ZThread::SuspendThread()
{
Sleep(INFINITE);
}inline HANDLE ZThread::GetHandle()
{
ZASSERT(m_ThreadData); return m_ThreadData->hThreadHandle;
}inline void ZThread::WakeUp()
{
ZASSERT(m_hWakeUpEvent); SetEvent(m_hWakeUpEvent);
}inline HANDLE ZThread::GetThreadSystemExitEvent()
{
ZASSERT(m_ThreadSystem != NULL); return m_ThreadSystem->GetExitEvent();
}#endif //#ifndef ZEPHYR_THREAD_H//////////////////////////////////////////////////////////////////////////////////////////////////////// netkernel.h----2// 服务器 网络核心#ifndef _ZEPHYR_NET_2_BASE_SERVER_H
#define _ZEPHYR_NET_2_BASE_SERVER_H#include "TBase/ZDebug.h"
#include "TBase/ZThread.h"
#include "BaseDef.h"
#include "NetStream.h"
#include "ZSocket.h"#ifndef TNET_API
#define TNET_API __declspec(dllimport)
#endif////////////////////////////////////////////////////////////////////////////////
// TNet
namespace TNet{ ////////////////////////////////////////////////////////////////////////////
// NetKernel
// 网络核心 class TNET_API NetKernel : public ZThread{
public:
// SendMsg
virtual int SendMsg(LPVOID pAddr,LPVOID pMsg,int msgLen) = 0; // 出错输出
virtual HRESULT Error(LPVOID pAddr,HRESULT hRet,const char* str) = 0; // 获得连接地址私有数据
virtual void SetData(LPVOID pAddr,LPVOID pData) = 0; // 获得连接地址私有数据
virtual LPVOID GetData(LPVOID pAddr) = 0;
}; typedef NetKernel* LPNetKernel; ////////////////////////////////////////////////////////////////////////////
// ServiceNetKernel
// 网络核心 class TNET_API ServiceNetKernel : public NetKernel{
public:
}; ////////////////////////////////////////////////////////////////////////////
// ClientNetKernel
// 网络核心 class TNET_API ClientNetKernel : public NetKernel{
public:
};
};#endif//_ZEPHYR_NET_2_BASE_SERVER_H
//////////////////////////////////////////////////////////////////////////////////////////////////////// tcpclient.h----3
因内容太长.请看附件