码迷,mamicode.com
首页 > 编程语言 > 详细

跨平台(win和unix)的线程封装类

时间:2018-06-18 11:02:10      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:span   stdcall   key   null   star   IV   pre   turn   ade   

  1. #ifdef WIN32  
  2. #include <Windows.h>  
  3. #include <process.h>  
  4. #else  
  5. #include <pthread.h>  
  6. #endif  
  7.   
  8. /* 
  9. #ifdef WIN32 
  10. typedef unsigned int(__stdcall *thread_func)(void*); 
  11. #else 
  12. typedef void*(*thread_func)(void*); 
  13. #endif 
  14. */  
  15.   
  16. class base_thread  
  17. {  
  18. public:  
  19.     base_thread();  
  20.     virtual ~base_thread();  
  21.   
  22.     bool create();  
  23.     void wait();  
  24.     virtual void run() = 0;  
  25.   
  26. #ifdef WIN32  
  27.     static unsigned __stdcall thread_func(void* arg);  
  28. #else  
  29.     static void* thread_func(void* arg);  
  30. #endif  
  31.   
  32. protected:  
  33. #ifdef WIN32  
  34.     HANDLE m_handle;  
  35. #else  
  36.     pthread_t m_thread_t;  
  37. #endif  
  38. };  
  39.   
  40. #endif  
[cpp] view plain copy
 
  1. base_thread::base_thread()  
  2. {  
  3. #ifdef WIN32  
  4.     m_handle = NULL;  
  5. #else  
  6.     m_thread_t = 0;  
  7. #endif  
  8. }  
  9.   
  10. base_thread::~base_thread()  
  11. {  
  12. #ifdef WIN32  
  13.     if (NULL != m_handle)  
  14.     {  
  15.         CloseHandle(m_handle);  
  16.     }  
  17.     m_handle = NULL;  
  18. #else  
  19.     m_thread_t = 0;  
  20. #endif  
  21. }  
  22.   
  23. bool base_thread::create()  
  24. {  
  25.     bool ret = false;  
  26. #ifdef WIN32  
  27.     m_handle = (HANDLE)_beginthreadex(NULL, 0, thread_func, this, 0, NULL);  
  28.     if (NULL != m_handle)  
  29.     {  
  30.         ret = true;  
  31.     }  
  32. #else  
  33.     if (0 == pthread_create(&m_thread_t, NULL, thread_func, this))  
  34.     {  
  35.         ret = true;  
  36.     }  
  37.     else  
  38.     {  
  39.         m_thread_t = 0;  
  40.     }  
  41. #endif  
  42.     return ret;  
  43. }  
  44.   
  45. void base_thread::wait()  
  46. {  
  47. #ifdef WIN32  
  48.     WaitForSingleObject(m_handle, INFINITE);  
  49.     if (NULL != m_handle)  
  50.     {  
  51.         CloseHandle(m_handle);  
  52.     }  
  53.     m_handle = NULL;  
  54. #else  
  55.     pthread_join(m_thread_t, NULL);  
  56.     m_thread_t = 0;  
  57. #endif // WIN32  
  58. }  
  59.   
  60. #ifdef WIN32  
  61. unsigned __stdcall base_thread::thread_func(void* arg)  
  62. #else  
  63. void* base_thread::thread_func(void* arg)  
  64. #endif  
  65. {  
  66.     base_thread *pthis = (base_thread*)arg;  
  67.     pthis->run();  
  68.     return NULL;  
  69. }  

封装了一个线程基类,可以在windows和linux下使用,其中run方法是要求继承的子类必须实现的,这个方法相当于线程函数,可以看到,在基类base_thread中,我在线程函数中调用了方法run。wait是用来等待线程安全退出放在主线程中卡住等待的。

跨平台(win和unix)的线程封装类

标签:span   stdcall   key   null   star   IV   pre   turn   ade   

原文地址:https://www.cnblogs.com/ostin/p/9194608.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!