KLThread.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:5k
源码类别:

模拟服务器

开发平台:

C/C++

  1. //---------------------------------------------------------------------------
  2. // Sword3 Engine (c) 1999-2000 by Kingsoft
  3. //
  4. // File: KLThread.cpp
  5. // Date: 2000.08.08
  6. // Code: Daniel Wang
  7. // Desc: BG2132 Font Class
  8. //---------------------------------------------------------------------------
  9. #include <stdafx.h>
  10. #include "KLThread.h"
  11. //---------------------------------------------------------------------------
  12. // 函数: KLThread
  13. // 功能: 购造函数
  14. // 参数: void
  15. // 返回: void
  16. //---------------------------------------------------------------------------
  17. KLThread::KLThread()
  18. {
  19. m_ThreadHandle = NULL;
  20. m_ThreadId = 0;
  21. m_ThreadFunc = NULL;
  22. m_ThreadParam = NULL;
  23. }
  24. //---------------------------------------------------------------------------
  25. // 函数: ~KLThread
  26. // 功能: 析购函数
  27. // 参数: void
  28. // 返回: void
  29. //---------------------------------------------------------------------------
  30. KLThread::~KLThread()
  31. {
  32. if (m_ThreadHandle)
  33. {
  34. CloseHandle(m_ThreadHandle);
  35. m_ThreadHandle = NULL;
  36. }
  37. }
  38. //---------------------------------------------------------------------------
  39. // 函数: ThreadProc
  40. // 功能: 线程过程
  41. // 参数: lpParam 参数
  42. // 返回: void
  43. //---------------------------------------------------------------------------
  44. DWORD WINAPI KLThread::ThreadProc(LPVOID lpParam)
  45. {
  46. KLThread* pThread = (KLThread*)lpParam;
  47. return pThread->ThreadFunction();
  48. }
  49. //---------------------------------------------------------------------------
  50. // 函数: ThreadFunction
  51. // 功能: 析购函数
  52. // 参数: void
  53. // 返回: void
  54. //---------------------------------------------------------------------------
  55. DWORD KLThread::ThreadFunction()
  56. {
  57. m_ThreadFunc(m_ThreadParam);
  58. return 0;
  59. }
  60. //---------------------------------------------------------------------------
  61. // 函数: Create
  62. // 功能: 创建线程
  63. // 参数: func 线程函数
  64. // arg  线程参数
  65. // 返回: BOOL
  66. //---------------------------------------------------------------------------
  67. BOOL KLThread::Create(TThreadFunc lpFunc, void* lpParam)
  68. {
  69. m_ThreadFunc   = lpFunc;
  70. m_ThreadParam  = lpParam;
  71. m_ThreadHandle = CreateThread(
  72. NULL, // SD
  73. 0, // initial stack size
  74. ThreadProc, // thread function
  75. this, // thread argument
  76. 0, // creation option
  77. &m_ThreadId); // thread identifier
  78. if(m_ThreadHandle)
  79. SetThreadPriority(m_ThreadHandle, THREAD_PRIORITY_ABOVE_NORMAL); 
  80. return (m_ThreadHandle != NULL);
  81. }
  82. //---------------------------------------------------------------------------
  83. // 函数: Destroy
  84. // 功能: 销毁线程
  85. // 参数: void
  86. // 返回: void
  87. //---------------------------------------------------------------------------
  88. void KLThread::Destroy()
  89. {
  90. TerminateThread(m_ThreadHandle, 0);
  91. }
  92. //---------------------------------------------------------------------------
  93. // 函数: Suspend
  94. // 功能: 挂起线程
  95. // 参数: void
  96. // 返回: void
  97. //---------------------------------------------------------------------------
  98. void KLThread::Suspend()
  99. {
  100.  SuspendThread(m_ThreadHandle);
  101. }
  102. //---------------------------------------------------------------------------
  103. // 函数: Resume
  104. // 功能: 唤醒线程
  105. // 参数: void
  106. // 返回: void
  107. //---------------------------------------------------------------------------
  108. void KLThread::Resume()
  109. {
  110. ResumeThread(m_ThreadHandle);
  111. }
  112. //---------------------------------------------------------------------------
  113. // 函数: IsRunning
  114. // 功能: 判断线程是否在运行
  115. // 参数: void
  116. // 返回: BOOL
  117. //---------------------------------------------------------------------------
  118. BOOL KLThread::IsRunning()
  119. {
  120. DWORD dwResult = WaitForSingleObject(m_ThreadHandle, 0);
  121. return (dwResult == WAIT_OBJECT_0);
  122. }
  123. //---------------------------------------------------------------------------
  124. // 函数: WaitForExit
  125. // 功能: 等待线程结束
  126. // 参数: void
  127. // 返回: void
  128. //---------------------------------------------------------------------------
  129. void KLThread::WaitForExit()
  130. {
  131. WaitForSingleObject(m_ThreadHandle, INFINITE);
  132. }
  133. //---------------------------------------------------------------------------
  134. // 函数: GetPriority
  135. // 功能: 取得线程优先级
  136. // 参数: void
  137. // 返回: int
  138. //---------------------------------------------------------------------------
  139. int KLThread::GetPriority()
  140. {
  141. return GetThreadPriority(m_ThreadHandle);
  142. }
  143. //---------------------------------------------------------------------------
  144. // 函数: SetPriority
  145. // 功能: 设置线程优先级
  146. // 参数: void
  147. // 返回: BOOL
  148. //---------------------------------------------------------------------------
  149. BOOL KLThread::SetPriority(int priority)
  150. {
  151. return SetThreadPriority(m_ThreadHandle, priority);
  152. }
  153. //---------------------------------------------------------------------------