123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- ///************************************************************************/
- /* Copyright (C), 2016-2020, [IT], 保留所有权利;
- /* 模 块 名:线程池;
- /* 描 述:;
- /*
- /* 版 本:[V];
- /* 作 者:[IT];
- /* 日 期:[3/7/2016];
- /*
- /*
- /* 注 意:;
- /*
- /* 修改记录:[IT];
- /* 修改日期:;
- /* 修改版本:;
- /* 修改内容:;
- /************************************************************************/
- #ifndef __THREADPOOL_20160307__
- #define __THREADPOOL_20160307__
- #pragma warning(push)
- #pragma warning(disable:4995)
- #include <memory>
- #pragma warning(pop)
- #pragma once
- class ThreadPool
- {
- static const int MAX_THREADS = 50;
- template <typename T>
- struct ThreadParam
- {
- void (T::* _function)(); T* _pobject;
- ThreadParam(void (T::* function)(), T * pobject)
- : _function(function), _pobject(pobject) { }
- };
- template <typename T>
- struct ThreadParamEx
- {
- void (T::* _function)(ULONG_PTR); T* _pobject; ULONG_PTR _arg;
- ThreadParamEx(void (T::* function)(ULONG_PTR), T * pobject, ULONG_PTR arg)
- : _function(function), _pobject(pobject), _arg(arg) { }
- };
- public:
- template <typename T>
- static bool QueueWorkItem(void (T::*function)(), T * pobject, ULONG nFlags = WT_EXECUTELONGFUNCTION)
- {
- std::auto_ptr< ThreadParam<T> > p(new ThreadParam<T>(function, pobject) );
- WT_SET_MAX_THREADPOOL_THREADS(nFlags, MAX_THREADS);
- bool result = false;
- if (::QueueUserWorkItem(WorkerThreadProc<T>, p.get(), nFlags))
- {
- p.release();
- result = true;
- }
- return result;
- }
- template <typename T>
- static bool QueueWorkItem(void (T::*function)(ULONG_PTR), T * pobject, ULONG_PTR arg, ULONG nFlags = WT_EXECUTELONGFUNCTION)
- {
- std::auto_ptr< ThreadParamEx<T> > p(new ThreadParamEx<T>(function, pobject, arg) );
- WT_SET_MAX_THREADPOOL_THREADS(nFlags, MAX_THREADS);
- bool result = false;
- if (::QueueUserWorkItem(WorkerThreadProcEx<T>, p.get(), nFlags))
- {
- p.release();
- result = true;
- }
- return result;
- }
- private:
- template <typename T>
- static DWORD WINAPI WorkerThreadProc(LPVOID pvParam)
- {
- std::auto_ptr< ThreadParam<T> > p(static_cast< ThreadParam<T>* >(pvParam));
- try
- {
- (p->_pobject->*p->_function)();
- }
- catch(...) {}
- return 0;
- }
- template <typename T>
- static DWORD WINAPI WorkerThreadProcEx(LPVOID pvParam)
- {
- std::auto_ptr< ThreadParamEx<T> > p(static_cast< ThreadParamEx<T>* >(pvParam));
- try
- {
- (p->_pobject->*p->_function)(p->_arg);
- }
- catch(...) {}
- return 0;
- }
- ThreadPool();
- };
- #endif // __THREADPOOL_20160307__
|