ThreadPool.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. ///************************************************************************/
  2. /* Copyright (C), 2016-2020, [IT], 保留所有权利;
  3. /* 模 块 名:线程池;
  4. /* 描 述:;
  5. /*
  6. /* 版 本:[V];
  7. /* 作 者:[IT];
  8. /* 日 期:[3/7/2016];
  9. /*
  10. /*
  11. /* 注 意:;
  12. /*
  13. /* 修改记录:[IT];
  14. /* 修改日期:;
  15. /* 修改版本:;
  16. /* 修改内容:;
  17. /************************************************************************/
  18. #ifndef __THREADPOOL_20160307__
  19. #define __THREADPOOL_20160307__
  20. #pragma warning(push)
  21. #pragma warning(disable:4995)
  22. #include <memory>
  23. #pragma warning(pop)
  24. #pragma once
  25. class ThreadPool
  26. {
  27. static const int MAX_THREADS = 50;
  28. template <typename T>
  29. struct ThreadParam
  30. {
  31. void (T::* _function)(); T* _pobject;
  32. ThreadParam(void (T::* function)(), T * pobject)
  33. : _function(function), _pobject(pobject) { }
  34. };
  35. template <typename T>
  36. struct ThreadParamEx
  37. {
  38. void (T::* _function)(ULONG_PTR); T* _pobject; ULONG_PTR _arg;
  39. ThreadParamEx(void (T::* function)(ULONG_PTR), T * pobject, ULONG_PTR arg)
  40. : _function(function), _pobject(pobject), _arg(arg) { }
  41. };
  42. public:
  43. template <typename T>
  44. static bool QueueWorkItem(void (T::*function)(), T * pobject, ULONG nFlags = WT_EXECUTELONGFUNCTION)
  45. {
  46. std::auto_ptr< ThreadParam<T> > p(new ThreadParam<T>(function, pobject) );
  47. WT_SET_MAX_THREADPOOL_THREADS(nFlags, MAX_THREADS);
  48. bool result = false;
  49. if (::QueueUserWorkItem(WorkerThreadProc<T>, p.get(), nFlags))
  50. {
  51. p.release();
  52. result = true;
  53. }
  54. return result;
  55. }
  56. template <typename T>
  57. static bool QueueWorkItem(void (T::*function)(ULONG_PTR), T * pobject, ULONG_PTR arg, ULONG nFlags = WT_EXECUTELONGFUNCTION)
  58. {
  59. std::auto_ptr< ThreadParamEx<T> > p(new ThreadParamEx<T>(function, pobject, arg) );
  60. WT_SET_MAX_THREADPOOL_THREADS(nFlags, MAX_THREADS);
  61. bool result = false;
  62. if (::QueueUserWorkItem(WorkerThreadProcEx<T>, p.get(), nFlags))
  63. {
  64. p.release();
  65. result = true;
  66. }
  67. return result;
  68. }
  69. private:
  70. template <typename T>
  71. static DWORD WINAPI WorkerThreadProc(LPVOID pvParam)
  72. {
  73. std::auto_ptr< ThreadParam<T> > p(static_cast< ThreadParam<T>* >(pvParam));
  74. try
  75. {
  76. (p->_pobject->*p->_function)();
  77. }
  78. catch(...) {}
  79. return 0;
  80. }
  81. template <typename T>
  82. static DWORD WINAPI WorkerThreadProcEx(LPVOID pvParam)
  83. {
  84. std::auto_ptr< ThreadParamEx<T> > p(static_cast< ThreadParamEx<T>* >(pvParam));
  85. try
  86. {
  87. (p->_pobject->*p->_function)(p->_arg);
  88. }
  89. catch(...) {}
  90. return 0;
  91. }
  92. ThreadPool();
  93. };
  94. #endif // __THREADPOOL_20160307__