Thread2.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "stdafx.h"
  2. #include <process.h>
  3. #include "Thread.h"
  4. #ifdef _DEBUG
  5. #define new DEBUG_NEW
  6. #undef THIS_FILE
  7. static char THIS_FILE[] = __FILE__;
  8. #endif
  9. unsigned int WINAPI ThreadProc(IN LPVOID lpParam);
  10. CThreadBase::CThreadBase()
  11. {
  12. m_nIndex = -1;
  13. m_hThread = NULL;
  14. m_ThreadStatus = THREADSTATUS_STOP;
  15. m_nThreadID = 0;
  16. m_hExit = NULL;
  17. m_hSemaphore = NULL;
  18. m_nSemaphoreNum = MAX_SEMAPHONE_NUM;
  19. }
  20. CThreadBase::~CThreadBase()
  21. {
  22. }
  23. /************************************************************************/
  24. /*
  25. 函数:Start
  26. 描述:启动线程
  27. 参数:
  28. 返回:1成功, 0失败
  29. */
  30. /************************************************************************/
  31. int CThreadBase::Start()
  32. {
  33. if(m_ThreadStatus != THREADSTATUS_STOP)
  34. return 1;
  35. m_hExit = CreateEvent(NULL, FALSE, FALSE, NULL);
  36. m_hSemaphore = CreateSemaphore(NULL, 0, MAX_SEMAPHONE_NUM, NULL);
  37. if(m_hThread == NULL)
  38. m_hThread = (HANDLE)_beginthreadex(NULL, 0, ThreadProc, this, 0, &m_nThreadID);
  39. if(m_hThread == 0)
  40. {
  41. CString strError = _T("");
  42. strError.Format(_T("创建线程失败:%d\n"), ::GetLastError());
  43. OutputDebugString(strError);
  44. return 0;
  45. }
  46. m_ThreadStatus = THREADSTATUS_WAIT;
  47. return 1;
  48. }
  49. void CThreadBase::Exit()
  50. {
  51. if(m_hExit)
  52. SetEvent(m_hExit);
  53. WakeUp();
  54. if(m_hThread)
  55. {
  56. #ifdef _DEBUG
  57. CString strMsg = _T("");
  58. strMsg.Format(_T("线程%d 等待退出\n"), m_nThreadID);
  59. OutputDebugString(strMsg);
  60. WaitForSingleObject(m_hThread, INFINITE);
  61. strMsg.Format(_T("线程%d 退出\n"), m_nThreadID);
  62. OutputDebugString(strMsg);
  63. #else
  64. WaitForSingleObject(m_hThread, INFINITE);
  65. #endif //#ifdef _DEBUG
  66. CloseHandle(m_hThread);
  67. }
  68. m_hThread = NULL;
  69. if(m_hSemaphore)
  70. CloseHandle(m_hSemaphore);
  71. m_hSemaphore = NULL;
  72. if(m_hExit)
  73. CloseHandle(m_hExit);
  74. m_hExit = NULL;
  75. }
  76. /************************************************************************/
  77. /*
  78. 函数:WakeUp
  79. 描述:唤醒
  80. 参数:
  81. 返回:1成功,0失败
  82. */
  83. /************************************************************************/
  84. void CThreadBase::WakeUp()
  85. {
  86. if(m_hSemaphore)
  87. ReleaseSemaphore(m_hSemaphore, 1, NULL);
  88. }
  89. /************************************************************************/
  90. /*
  91. 函数:WorkItem
  92. 描述:工作项
  93. 参数:
  94. IN LPVOID lpParam
  95. 返回:0
  96. */
  97. /************************************************************************/
  98. unsigned int WINAPI ThreadProc(IN LPVOID lpParam)
  99. {
  100. CThreadBase* p = (CThreadBase*)lpParam;
  101. int nSatus = 0;
  102. while(1)
  103. {
  104. int nSatus = p->Run();
  105. if(nSatus == THREADSTATUS_EXIT) // 程序结束
  106. break;
  107. }
  108. return 0;
  109. }