1 ////////////////////////////////////////
5 // 1. Declair the CThread class that encapsulates threading functionality.
6 // This abstract base class in intended to be used to encapsulate
7 // individual tasks that require threading in derived classes.
13 #ifndef BASE_SOLARIS_THREAD_H
14 #define BASE_SOLARIS_THREAD_H
16 #if !defined(_REENTRANT)
17 # pragma message( "Excluding Base::CThread - requires multi-threaded compile. (_REENTRANT)" )
21 #pragma warning( disable : 4786)
32 ////////////////////////////////////////
37 // Encapsulates threading functionality. Creating classes derived
38 // from CThread provides an easy way to encapsulate tasks that require
42 // StartThread() : Creates the low-level thread handle and begins executing
43 // the CThread::ThreadProc() function within the new thread.
44 // StopThread() : Signals the ThreadProc() function to stop executing using
45 // the mThreadContinue member variable, and waits for the ThreadProc()
46 // function to exit. By default, the function will block for a maximum
47 // of 5 seconds before exiting without the thread halting.
48 // IsThreadActive() : Returns true if the physical thread is still executing
49 // within the ThreadProc() function, otherwise it returns false.
50 // ThreadProc() : Pure-virtual function that will be executed when the StartThread()
51 // function is called. Derived classes must implement this function. The
52 // mThreadContinue member variable should be used internal the the ThreadProc()
53 // function to indicate whether it should continue executing or exit.
54 // Protected Attributes:
55 // mThreadContinue : Boolean value indicating to the ThreadProc() function
56 // whether to continue executing or to exit. If mThreadContinue is true,
57 // ThreadProc() should continue, otherwise ThreadProc() should exit. It
58 // left up to the derived class to implement a ThreadProc() function that
59 // uses the mThreadContinue member.
64 friend void * threadProc(void *);
67 enum { eSTOP_SUCCESS
, eSTOP_TIMEOUT
};
73 int32
StopThread(int timeout
=5);
74 bool IsThreadActive() { return mThreadActive
; }
77 virtual void ThreadProc() = 0;
90 class CMember
: public CThread
93 CMember(CThreadPool
* parent
);
96 bool Execute(void( *function
)( void * ), void * arg
);
100 virtual void ThreadProc();
103 CThreadPool
* mParent
;
104 void( * mFunction
)( void * );
108 friend class CMember
;
111 CThreadPool(uint32 maxThreads
, uint32 minThreads
=1, uint32 timeout
=15*60);
114 bool Execute(void( *function
)( void * ), void * arg
);
118 void OnIdle(CMember
* member
);
119 void OnBusy(CMember
* member
);
120 void OnDestory(CMember
* member
);
124 std::set
<CMember
*> mIdleMember
;
125 std::set
<CMember
*> mBusyMember
;
126 std::list
<CMember
*> mNullMember
;
137 #endif // #if defined(_MT)
139 #endif // BASE_SOLARIS_THREAD_H