?? threadpool.h
字號:
/*///////////////////////////////////////////////////////////////////////////////////
線程管理
包括線程池管理
線程隊列管理
線程封裝
李亦
2006.6.15
/*///////////////////////////////////////////////////////////////////////////////////
#ifndef _THREADPOOL_H_
#define _THREADPOOL_H_
#ifndef _PLATFORMSEMAPHORE_H_
#include "platform/platformMutex.h"
#endif
#ifndef _METHODDISPATCH_H_
#include "server/methodDispatch.h"
#endif
#ifndef _PLATFORMSEMAPHORE_H_
#include "platform/platformSemaphore.h"
#endif
#ifndef _PLATFORMTHREAD_H_
#include "platform/platformThread.h"
#endif
//namespace RPGServer
//{
/// Platform independent per-thread storage class.
class ThreadStorage
{
U32 mTlsIndex;
public:
/// ThreadStorage constructor.
ThreadStorage();
/// ThreadStorage destructor.
~ThreadStorage();
/// returns the per-thread stored void pointer for this ThreadStorage. The default value is NULL.
void *get();
/// sets the per-thread stored void pointer for this ThreadStorage object.
void set(void *data);
};
/// Managing object for a queue of worker threads that pass
/// messages back and forth to the main thread. ThreadQueue
/// methods declared with the TNL_DECLARE_THREADQ_METHOD macro
/// are special -- if they are called from the main thread,
/// they will be executed on one of the worker threads and vice
/// versa.
class ThreadQueue //: public Object
{
class ThreadQueueThread : public Thread
{
friend class ThreadQueue;
U32 m_uExpire;
//BOOL m_bStop;
ThreadQueue *mThreadQueue;
public:
ThreadQueueThread(ThreadQueue *);
virtual void run(S32 arg);
};
friend class ThreadQueueThread;
U32 mLifeTime; //空閑線程生命周期
U32 mMaxAmount; //線程最大數量,工作線程數量超過此數,則需要排隊等候
U32 mMinAmount; //線程最小數量,小過此數,空閑線程將不被銷毀
S32 mWorkingNum; //工作狀態線程數量
Vector<Thread *> mThreads; /// ThreadQueue工作線程列表
Vector<Functor *> mThreadCalls; /// 工作線程執行函數列表
Vector<Functor *> mResponseCalls;/// 工作線程響應函數列表
SemaphoreInstance mTaskSemaphore; /// 工作線程工作協調信號管理
MutexInstance mMutex; /// 工作線程執行函數調用信號管理
/// Storage variable that tracks whether this is the main thread or a worker thread.
//ThreadStorage mStorage;
public:
enum
{
NODEAD = -1,
DEADNOW = 0,
DEFAULT = 30000,//默認的生命周期 30s
};
protected:
/// 隊列成員訪問存取鎖
bool lock() { return mMutex.lock(true); }
void unlock() { mMutex.unlock(); }
/// Posts a marshalled call onto either the worker thread call list or the response call list.
void postCall(Functor *theCall);
/// Dispatches the next available worker thread call. Called internally by the worker threads when they awaken from the semaphore.
bool dispatchNextCall();
/// helper function to determine if the currently executing thread is a worker thread or the main thread.
//bool isMainThread();
//ThreadStorage &getStorage();
/// called by each worker thread when it starts for subclass initialization of worker threads.
virtual void threadStart(){}
//新建一線程,并自動加到線程池中
//如果創建后總數超過nMax,則創建失敗
bool newThread();
bool removeThread(ThreadQueueThread* pThread);
public:
/// ThreadQueue constructor. threadCount specifies the number of worker threads that will be created.
ThreadQueue(U32 uInit,U32 nMin, U32 nMax,U32 uLife=DEFAULT);
~ThreadQueue();
/// Dispatches all ThreadQueue calls queued by worker threads. This should
/// be called periodically from a main loop.
void dispatchResponseCalls();
};
//////////////////////////////////////////////////////////////
//inline bool ThreadQueue::isMainThread()
//{
// return (bool) mStorage.get();
//}
//inline ThreadStorage &ThreadQueue::getStorage()
//{
// return mStorage;
//}
//
//////////////////////////////////////////////////////////////
/// Declares a ThreadQueue method on a subclass of ThreadQueue.
#define DECLARE_THREADQ_METHOD(func, args) \
void func args; \
void func##_body args
/// Declares the implementation of a ThreadQueue method.
#define IMPLEMENT_THREADQ_METHOD(className, func, args, argNames) \
void className::func args { \
FunctorDecl<void (className::*)args> *theCall = new FunctorDecl<void (className::*)args>(&className::func##_body); \
theCall->set argNames; \
postCall(theCall); \
}\
void className::func##_body args
//};//RPGServer
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -