ThreadPoolModel.cpp
上传用户:swkcbjrc
上传日期:2016-04-02
资源大小:45277k
文件大小:3k
源码类别:

游戏

开发平台:

Visual C++

  1. // ThreaoolModel.cpp: implementation of the CThreadPoolModel class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "ThreadpoolModel.h"
  6. #include "globfunction.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. const DWORD _THREADDEAD_TIMEOUT= 4*1000;// 4秒线程死亡超时
  16. CThreadPoolModel::CThreadPoolModel()
  17. {
  18. InitAll();
  19. }
  20. CThreadPoolModel::~CThreadPoolModel()
  21. {
  22. DeleteAll();
  23. }
  24. //显示当前线程数量
  25. //input: void
  26. //output: 返回线程情况串
  27. CString CThreadPoolModel::ShowThreadInfo(void)
  28. {
  29. CString strShow;
  30. strShow.Format(
  31. "最大线程=%d,当前线程=%d,工作线程=%d,临界线程=%dn",
  32. m_nMaxThreadCount,m_nCurAllThreadCount,
  33. m_nCurWorkThreadCount,m_nCriThreadCount);
  34. return strShow;
  35. }
  36. //初始化所有信息
  37. //input: void
  38. //output: void
  39. void CThreadPoolModel::InitAll(void)
  40. {
  41. InitCriticalSection();
  42. m_nMaxThreadCount=0;
  43. m_nCurAllThreadCount=0;
  44. m_nCurWorkThreadCount=0;
  45. m_nErrorNo=0;
  46. m_hAllDeadEvent=NULL;
  47. m_hMainWnd=NULL;
  48. m_hAllDeadEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
  49. m_nCriThreadCount=0;
  50. }
  51. //删除所有包括句柄
  52. //input: void
  53. //output: void
  54. void CThreadPoolModel::DeleteAll(void)
  55. {
  56. DestroyCriticalSection();
  57. CloseHandle(m_hAllDeadEvent);
  58. }
  59. void CThreadPoolModel::InitCriticalSection(void)
  60. {
  61. ::InitializeCriticalSection(&m_csErrorNo);
  62. }
  63. void CThreadPoolModel::DestroyCriticalSection(void)
  64. {
  65. ::DeleteCriticalSection(&m_csErrorNo);
  66. }
  67. //创建新线程
  68. //input: 线程函数指针,参数指针,线程句柄地址,线程ID地址
  69. //output: TRUE/FASLE
  70. BOOL CThreadPoolModel::CreateNewThread(lpCallBack *pfnThrearoc ,LPVOID pParam,
  71.   HANDLE &hNewThread,
  72.   DWORD &dwThreadId)
  73. {
  74. //如果当前工作线程大于或等于临界线程量,那么我们创建新的线程
  75. if(m_nCurAllThreadCount>=m_nMaxThreadCount)
  76. return FALSE;
  77. if(pfnThrearoc==NULL)
  78. {
  79. TPSetLastError(ERROR_THREADPOOL_FULL);
  80. return FALSE;
  81. }
  82. DWORD dwThreadIdTemp=0;
  83. //创建新线程
  84. hNewThread=chBEGINTHREADEX(NULL,0,pfnThrearoc,pParam,0,&dwThreadIdTemp);
  85. dwThreadId=dwThreadIdTemp;
  86. //增加当前存在线程数量
  87. IncrementCurAllThreadCount();
  88. return TRUE;
  89. }
  90. void CThreadPoolModel::IncrementCurAllThreadCount(void)
  91. {::InterlockedIncrement((LPLONG)&m_nCurAllThreadCount);}
  92. void CThreadPoolModel::DecrementCurAllThreadCount(void)
  93. {::InterlockedDecrement((LPLONG)&m_nCurAllThreadCount);}
  94. void CThreadPoolModel::SetCurAllThreadCount(int nCount)
  95. {::InterlockedExchange((LPLONG)&m_nCurAllThreadCount,nCount);}
  96. void CThreadPoolModel::IncrementCurWorkThreadCount(void)
  97. {
  98. ::InterlockedIncrement((LPLONG)&m_nCurWorkThreadCount);
  99. }
  100. void CThreadPoolModel::DecrementCurWorkThreadCount(void)
  101. {::InterlockedDecrement((LPLONG)&m_nCurWorkThreadCount);}
  102. void CThreadPoolModel::SetCurWorkThreadCount(int nCount)
  103. {::InterlockedExchange((LPLONG)&m_nCurWorkThreadCount,nCount);}
  104. void CThreadPoolModel::IncrementCriThreadCount(void)
  105. {::InterlockedIncrement((LPLONG)&m_nCriThreadCount);}
  106. void CThreadPoolModel::DecrementCriThreadCount(void)
  107. {::InterlockedDecrement((LPLONG)&m_nCriThreadCount);}
  108. void CThreadPoolModel::SetCriThreadCount(int nCount)
  109. {::InterlockedExchange((LPLONG)&m_nCriThreadCount,nCount);}