简单的线程池

    技术2022-05-20  29

          一个简单的线程池的实现,基本原理:创建线程池的时候即创建多个线程,且让它们处于挂起状态,然后等待用户需要创建一个线程的时候,恢复其中一个被挂起的线程,并执行用户指定的线程函数。执行完毕后再次挂起这个线程,直到用户再次需要创建新的线程的时候,再恢复一个挂起的线程。最后程序退出,关闭所有线程。

     

    ThreadPool 头文件:

    #ifndef __ThreadPool_h__ #define __ThreadPool_h__ #include <Windows.h> typedef unsigned int (__stdcall *PTHREADFUNC)(void*); typedef enum _thread_state { TS_RUNNING, TS_SUSPENDED, TS_FINISHED } ThreadState; typedef struct _thread_info { HANDLE _Handle; DWORD _Id; DWORD _ExitCode; ThreadState _State; PTHREADFUNC _ThrdFunc; void *_Param; } ThreadInfo; class CThreadPool { public: CThreadPool( long nMaxCount ); ~CThreadPool(); bool Create (); void Destroy (); bool IsRunning () { return _IsCreated; } int CreateThread ( PTHREADFUNC pThrdFunc, void *pParam, ULONG flag ); void ResumeThread ( int index ); void SuspendThread ( int index ); void EndThread ( int index ); private: bool _IsCreated; long _MaxCount; long _CurIndex; ThreadInfo *_ThreadList; CRITICAL_SECTION _CS; static void ResetThrdInfo ( ThreadInfo *pThrdInfo ); static unsigned int __stdcall DefThrdFunc ( void *pParam ); int GetThread() { int i; for( i = _CurIndex; i < _MaxCount; i++ ) { if( _ThreadList[i]._State == TS_FINISHED ) { _CurIndex = (i + 1) % _MaxCount; return i; } } for( i = 0; i < _CurIndex; i++ ) { if( _ThreadList[i]._State == TS_FINISHED ) { _CurIndex = (i + 1) % _MaxCount; return i; } } return -1; } }; #endif // __ThreadPool_h__

     

    ThreadPool 源文件:

    #include "ThreadPool.h" unsigned int __stdcall CThreadPool::DefThrdFunc( void *pParam ) { ThreadInfo *pThrdInfo = (ThreadInfo*)pParam; if( NULL != pThrdInfo ) { while( 1 ) { if( TS_RUNNING == pThrdInfo->_State ) { pThrdInfo->_ThrdFunc( pThrdInfo->_Param ); ResetThrdInfo( pThrdInfo ); } } } return 0; } void CThreadPool::ResetThrdInfo( ThreadInfo *pThrdInfo ) { if( NULL != pThrdInfo ) { pThrdInfo->_State = TS_FINISHED; pThrdInfo->_ThrdFunc = DefThrdFunc; pThrdInfo->_Param = NULL; pThrdInfo->_ExitCode = 0; ::SuspendThread( pThrdInfo->_Handle ); } } CThreadPool::CThreadPool( long nMaxCount ) : _MaxCount(nMaxCount), _CurIndex(-1), _IsCreated(false), _ThreadList(NULL) { } CThreadPool::~CThreadPool() { if( _IsCreated ) Destroy(); } bool CThreadPool::Create() { if( _IsCreated ) return true; InitializeCriticalSection( &_CS ); _ThreadList = new ThreadInfo[_MaxCount]; memset( _ThreadList, 0, sizeof(ThreadInfo) * _MaxCount ); _IsCreated = true; for( int i = 0; i < _MaxCount; i++ ) { DWORD dwId; HANDLE h = ::CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)DefThrdFunc, &_ThreadList[i], CREATE_SUSPENDED, &dwId ); if( NULL == h ) { Destroy(); return false; } else { EnterCriticalSection( &_CS ); _ThreadList[i]._ThrdFunc = DefThrdFunc; _ThreadList[i]._Param = NULL; _ThreadList[i]._Handle = h; _ThreadList[i]._Id = dwId; _ThreadList[i]._State = TS_FINISHED; _ThreadList[i]._ExitCode = 0; LeaveCriticalSection( &_CS ); } } _CurIndex = 0; return true; } void CThreadPool::Destroy() { if( !_IsCreated || NULL == _ThreadList ) return; EnterCriticalSection( &_CS ); for( int i = 0; i < _MaxCount; i++ ) { if( _ThreadList[i]._Handle ) { TerminateThread( _ThreadList[i]._Handle, _ThreadList[i]._ExitCode ); CloseHandle( _ThreadList[i]._Handle ); } } LeaveCriticalSection( &_CS ); _IsCreated = false; _MaxCount = 0; _CurIndex = -1; delete []_ThreadList; _ThreadList = NULL; DeleteCriticalSection( &_CS ); } int CThreadPool::CreateThread( PTHREADFUNC pThrdFunc, void *pParam, ULONG flag ) { if( NULL == pThrdFunc ) return NULL; int index = GetThread(); if( index < 0 ) return -1; EnterCriticalSection( &_CS ); _ThreadList[index]._ThrdFunc = pThrdFunc; _ThreadList[index]._Param = pParam; if( CREATE_SUSPENDED == flag ) _ThreadList[index]._State = TS_SUSPENDED; else { _ThreadList[index]._State = TS_RUNNING; ::ResumeThread( _ThreadList[index]._Handle ); } LeaveCriticalSection( &_CS ); return index; } void CThreadPool::ResumeThread( int index ) { if( index < 0 || index >= _MaxCount ) return; if( NULL != _ThreadList ) { if( TS_SUSPENDED == _ThreadList[index]._State ) ::ResumeThread( _ThreadList[index]._Handle ); } } void CThreadPool::SuspendThread( int index ) { if( index < 0 || index >= _MaxCount ) return; if( NULL != _ThreadList ) { if( TS_RUNNING == _ThreadList[index]._State ) ::SuspendThread( _ThreadList[index]._Handle ); } } void CThreadPool::EndThread( int index ) { if( index < 0 || index >= _MaxCount ) return; if( NULL == _ThreadList ) return; if( TS_RUNNING == _ThreadList[index]._State ) ::SuspendThread( _ThreadList[index]._Handle ); EnterCriticalSection( &_CS ); _ThreadList[index]._State = TS_FINISHED; _ThreadList[index]._ThrdFunc = DefThrdFunc; _ThreadList[index]._Param = NULL; LeaveCriticalSection( &_CS ); }

     

    测试代码:

    #include "ThreadPool.h" #include <stdio.h> unsigned int __stdcall thread_proc1( void *param ) { int *pindex = (int*)param; printf( "thread_proc1: thread %d running .../r/n", *pindex ); return 0; } unsigned int __stdcall thread_proc2( void *param ) { int *pindex = (int*)param; printf( "thread_proc2: thread %d running .../r/n", *pindex ); return 0; } unsigned int __stdcall thread_proc3( void *param ) { int *pindex = (int*)param; printf( "thread_proc3: thread %d running .../r/n", *pindex ); return 0; } int main( void ) { CThreadPool pool( 12 ); if( !pool.Create() ) return 1; int arr[30] = { 0 }; int i; for( i = 0; i < 30; i++ ) arr[i] = i + 1; for( i = 0; i < 10; i++ ) pool.CreateThread( (PTHREADFUNC)thread_proc1, (void*)&arr[i], 0 ); Sleep( 300 ); for( i = 10; i < 20; i++ ) pool.CreateThread( (PTHREADFUNC)thread_proc2, (void*)&arr[i], 0 ); Sleep( 1000 ); for( i = 20; i < 30; i++ ) pool.CreateThread( (PTHREADFUNC)thread_proc3, (void*)&arr[i], 0 ); Sleep( 2000 ); pool.Destroy(); return 0; }

     

    执行结果:

    thread_proc1: thread 1 running ... thread_proc1: thread 2 running ... thread_proc1: thread 4 running ... thread_proc1: thread 6 running ... thread_proc1: thread 3 running ... thread_proc1: thread 8 running ... thread_proc1: thread 10 running ... thread_proc1: thread 5 running ... thread_proc1: thread 7 running ... thread_proc1: thread 9 running ... thread_proc2: thread 14 running ... thread_proc2: thread 11 running ... thread_proc2: thread 13 running ... thread_proc2: thread 15 running ... thread_proc2: thread 12 running ... thread_proc2: thread 17 running ... thread_proc2: thread 19 running ... thread_proc2: thread 16 running ... thread_proc2: thread 18 running ... thread_proc2: thread 20 running ... thread_proc3: thread 21 running ... thread_proc3: thread 22 running ... thread_proc3: thread 23 running ... thread_proc3: thread 25 running ... thread_proc3: thread 27 running ... thread_proc3: thread 29 running ... thread_proc3: thread 24 running ... thread_proc3: thread 26 running ... thread_proc3: thread 28 running ... thread_proc3: thread 30 running ... 请按任意键继续. . .


    最新回复(0)