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

模拟服务器

开发平台:

C/C++

  1. //---------------------------------------------------------------------------
  2. // Sword3 Engine (c) 1999-2000 by Kingsoft
  3. //
  4. // File: KThread.cpp
  5. // Date: 2000.08.08
  6. // Code: WangWei(Daphnis)
  7. // Desc: BG2132 Font Class
  8. //---------------------------------------------------------------------------
  9. #include "KWin32.h"
  10. #include "KDebug.h"
  11. #include "KMemBase.h"
  12. #include "KThread.h"
  13. #ifdef WIN32
  14. #include "process.h"
  15. #endif
  16. KThread::KThread()
  17. {
  18. #ifdef WIN32
  19. m_ThreadHandle = NULL;
  20. m_ThreadId = 0;
  21. m_ThreadFunc = NULL;
  22. m_ThreadParam = NULL;
  23. #endif
  24. }
  25. //---------------------------------------------------------------------------
  26. // 函数: ~KThread
  27. // 功能: 析购函数
  28. // 参数: void
  29. // 返回: void
  30. //---------------------------------------------------------------------------
  31. KThread::~KThread()
  32. {
  33. #ifdef WIN32
  34. if (m_ThreadHandle)
  35. {
  36. CloseHandle(m_ThreadHandle);
  37. m_ThreadHandle = NULL;
  38. }
  39. #else
  40. #endif
  41. }
  42. //---------------------------------------------------------------------------
  43. // 函数: MyThreadProc
  44. // 功能: 线程过程
  45. // 参数: lpParam 参数
  46. // 返回: void
  47. //---------------------------------------------------------------------------
  48. #ifdef WIN32
  49. unsigned __stdcall MyThreadProc(LPVOID lpParam)
  50. #else
  51. void * MyThreadProc(LPVOID lpParam)
  52. #endif
  53. {
  54.  printf("start thread %dn", lpParam);
  55. KThread* pThread = (KThread*)lpParam;
  56. #ifdef WIN32
  57. return pThread->ThreadFunction();
  58. #else
  59. return (void *)pThread->ThreadFunction();
  60. #endif
  61. // return 0;
  62. }
  63. //---------------------------------------------------------------------------
  64. // 函数: ThreadFunction
  65. // 功能: 析购函数
  66. // 参数: void
  67. // 返回: void
  68. //---------------------------------------------------------------------------
  69. DWORD KThread::ThreadFunction()
  70. {
  71. m_ThreadFunc(m_ThreadParam);
  72. return 0;
  73. }
  74. //---------------------------------------------------------------------------
  75. // 函数: Create
  76. // 功能: 创建线程
  77. // 参数: func 线程函数
  78. // arg  线程参数
  79. // 返回: BOOL
  80. //---------------------------------------------------------------------------
  81. BOOL KThread::Create(TThreadFunc lpFunc, void* lpParam)
  82. {
  83. #ifdef WIN32
  84. m_ThreadFunc   = lpFunc;
  85. m_ThreadParam  = lpParam;
  86. m_ThreadHandle = (HANDLE)_beginthreadex(
  87. NULL, // SD
  88. 0, // initial stack size
  89. MyThreadProc, // thread function
  90. this, // thread argument
  91. 0, // creation option
  92. (unsigned*)&m_ThreadId); // thread identifier
  93. return (m_ThreadHandle != NULL);
  94. #else
  95.      m_ThreadFunc   = lpFunc;
  96.      int ret = pthread_create(&p_thread, NULL, MyThreadProc, this);
  97.      printf("create thread %d return %dn", (int)p_thread, ret);
  98. #endif
  99. // return FALSE;
  100. }
  101. //---------------------------------------------------------------------------
  102. // 函数: Destroy
  103. // 功能: 销毁线程
  104. // 参数: void
  105. // 返回: void
  106. //---------------------------------------------------------------------------
  107. void KThread::Destroy()
  108. {
  109. #ifdef WIN32
  110. TerminateThread(m_ThreadHandle, 0);
  111. #endif
  112. }
  113. //---------------------------------------------------------------------------
  114. // 函数: Suspend
  115. // 功能: 挂起线程
  116. // 参数: void
  117. // 返回: void
  118. //---------------------------------------------------------------------------
  119. void KThread::Suspend()
  120. {
  121. #ifdef WIN32
  122.  SuspendThread(m_ThreadHandle);
  123. #endif
  124. }
  125. //---------------------------------------------------------------------------
  126. // 函数: Resume
  127. // 功能: 唤醒线程
  128. // 参数: void
  129. // 返回: void
  130. //---------------------------------------------------------------------------
  131. void KThread::Resume()
  132. {
  133. #ifdef WIN32
  134. ResumeThread(m_ThreadHandle);
  135. #endif
  136. }
  137. //---------------------------------------------------------------------------
  138. // 函数: IsRunning
  139. // 功能: 判断线程是否在运行
  140. // 参数: void
  141. // 返回: BOOL
  142. //---------------------------------------------------------------------------
  143. BOOL KThread::IsRunning()
  144. {
  145. #ifdef WIN32
  146. DWORD dwResult = WaitForSingleObject(m_ThreadHandle, 0);
  147. return (dwResult == WAIT_OBJECT_0);
  148. #endif
  149. // return FALSE;
  150. }
  151. //---------------------------------------------------------------------------
  152. // 函数: WaitForExit
  153. // 功能: 等待线程结束
  154. // 参数: void
  155. // 返回: void
  156. //---------------------------------------------------------------------------
  157. void KThread::WaitForExit()
  158. {
  159. #ifdef WIN32
  160. WaitForSingleObject(m_ThreadHandle, INFINITE);
  161. #endif
  162. }
  163. //---------------------------------------------------------------------------
  164. // 函数: GetPriority
  165. // 功能: 取得线程优先级
  166. // 参数: void
  167. // 返回: int
  168. //---------------------------------------------------------------------------
  169. int KThread::GetPriority()
  170. {
  171. #ifdef WIN32
  172. return GetThreadPriority(m_ThreadHandle);
  173. #endif
  174. // return 0;
  175. }
  176. //---------------------------------------------------------------------------
  177. // 函数: SetPriority
  178. // 功能: 设置线程优先级
  179. // 参数: void
  180. // 返回: BOOL
  181. //---------------------------------------------------------------------------
  182. BOOL KThread::SetPriority(int priority)
  183. {
  184. #ifdef WIN32
  185. return SetThreadPriority(m_ThreadHandle, priority);
  186. #endif
  187. // return FALSE;
  188. }