thread.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #pragma once
  2. #include <functional>
  3. #include <thread>
  4. #include "json.h"
  5. namespace ylib
  6. {
  7. /****************************************************
  8. * Class:线程工具类
  9. * DateTime:2019-10-29
  10. ****************************************************/
  11. class ithread
  12. {
  13. public:
  14. ithread();
  15. ~ithread();
  16. /***************************************************************************
  17. * Function:开启线程
  18. * Return:
  19. * == true : 成功
  20. * == false : 失败
  21. ****************************************************************************/
  22. bool start();
  23. void wait();
  24. /***************************************************************************
  25. * Function:停止线程
  26. * Return:
  27. * == true : 成功
  28. * == false : 失败
  29. ****************************************************************************/
  30. bool stop();
  31. /// <summary>
  32. /// 等待关闭
  33. /// </summary>
  34. bool wait_stop();
  35. /// <summary>
  36. /// 睡眠等待
  37. /// </summary>
  38. void sleep(int32 msec);
  39. private:
  40. /***************************************************************************
  41. * Function:纯虚函数 需要子类实现
  42. * Description:本类会线程内调用该类,间歇时间由 Run() 内自行实现
  43. * Return:
  44. * == true : 继续执行下一循环
  45. * == false : 终止循环,退出线程
  46. ****************************************************************************/
  47. virtual bool run() = 0;
  48. public:
  49. void* __thread_handle(void* param);
  50. public:
  51. // 0=启动中 1=停止信号已发送 2=已停止
  52. unsigned int m_state;
  53. bool m_thread;
  54. };
  55. }