⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threadpool.h

📁 五行MMORPG引擎系统V1.0
💻 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 + -