tc_thread.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #ifndef __TC_THREAD_H_
  17. #define __TC_THREAD_H_
  18. #include <sys/types.h>
  19. #include <signal.h>
  20. // #include <pthread.h>
  21. #include <thread>
  22. #include "util/tc_ex.h"
  23. #include "util/tc_monitor.h"
  24. namespace tars
  25. {
  26. /////////////////////////////////////////////////
  27. /**
  28. * @file tc_thread.h
  29. * @brief 线程类(兼容TAF4.x版本, 底层直接封装了c++11 thread, 从而跨平台兼容)
  30. *
  31. * @author jarodruan@upchina.com
  32. */
  33. /////////////////////////////////////////////////
  34. /**
  35. * @brief 线程控制异常类
  36. */
  37. struct TC_ThreadThreadControl_Exception : public TC_Exception
  38. {
  39. TC_ThreadThreadControl_Exception(const string &buffer) : TC_Exception(buffer){};
  40. ~TC_ThreadThreadControl_Exception() throw() {};
  41. };
  42. /**
  43. * @brief 线程控制类
  44. */
  45. class TC_ThreadControl
  46. {
  47. public:
  48. /**
  49. * @brief 构造, 表示当前运行的线程,
  50. * join和detach在不能在该对象上调用
  51. */
  52. explicit TC_ThreadControl(std::thread *th);
  53. /**
  54. * @brief 等待当前线程结束, 不能在当前线程上调用
  55. */
  56. void join();
  57. /**
  58. * @brief detach, 不能在当前线程上调用
  59. */
  60. void detach();
  61. /**
  62. * @brief 获取当前线程id.
  63. *
  64. * @return pthread_t当前线程id
  65. */
  66. std::thread::id id() const;
  67. /**
  68. * @brief 休息ms时间.
  69. *
  70. * @param millsecond 休息的时间,具体ms数字
  71. */
  72. static void sleep(int64_t millsecond);
  73. /**
  74. * @brief 交出当前线程控制权
  75. */
  76. static void yield();
  77. private:
  78. std::thread *_th;
  79. };
  80. /**
  81. *
  82. */
  83. class TC_Runable
  84. {
  85. public:
  86. virtual ~TC_Runable(){};
  87. virtual void run() = 0;
  88. };
  89. /**
  90. * @brief 线程基类.
  91. * 线程基类,所有自定义线程继承于该类,同时实现run接口即可,
  92. *
  93. * 可以通过TC_ThreadContorl管理线程。
  94. */
  95. class TC_Thread : public TC_Runable
  96. {
  97. public:
  98. /**
  99. * @brief 构造函数
  100. */
  101. TC_Thread();
  102. /**
  103. * @brief 析构函数
  104. */
  105. virtual ~TC_Thread();
  106. /**
  107. * @brief 线程运行
  108. */
  109. TC_ThreadControl start();
  110. /**
  111. * @brief 获取线程控制类.
  112. *
  113. * @return ThreadControl
  114. */
  115. TC_ThreadControl getThreadControl();
  116. /**
  117. * @brief 线程是否存活.
  118. *
  119. * @return bool 存活返回true,否则返回false
  120. */
  121. bool isAlive() const;
  122. /**
  123. * @brief 获取线程id.
  124. *
  125. * @return std::thread::id 线程id
  126. */
  127. std::thread::id id() { return _th->get_id(); }
  128. /**
  129. * @brief 获取线程
  130. *
  131. * @return std::thread::id 线程id
  132. */
  133. std::thread* getThread() { return _th; }
  134. /**
  135. * @brief 获取当前线程ID, 用size_t返回
  136. *
  137. * @return
  138. */
  139. static size_t CURRENT_THREADID();
  140. protected:
  141. /**
  142. * @brief 静态函数, 线程入口.
  143. *
  144. * @param pThread 线程对象
  145. */
  146. static void threadEntry(TC_Thread *pThread);
  147. /**
  148. * @brief 运行
  149. */
  150. virtual void run() = 0;
  151. protected:
  152. /**
  153. * 是否在运行
  154. */
  155. bool _running;
  156. /**
  157. * 线程锁
  158. */
  159. TC_ThreadLock _lock;
  160. //当前线程
  161. std::thread *_th;
  162. };
  163. }
  164. #endif