zoukankan      html  css  js  c++  java
  • 跨平台(win和unix)的线程封装类

    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是用来等待线程安全退出放在主线程中卡住等待的。

  • 相关阅读:
    Ubuntu设置root用户登录图形界面
    Hadoop的安装与设置(1)
    SSH框架和Redis的整合(1)
    @Autowired注解的使用
    The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
    Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...
    java.lang.NoSuchFieldError: org.apache.http.message.BasicLineFormatter.INSTANCE
    Android Studio的SVN Performing VCS Refresh/Commit 长时间不结束
    Error:Execution failed for task ':app:clean'.
    Android Studio项目提交到GitHub
  • 原文地址:https://www.cnblogs.com/ostin/p/9194608.html
Copyright © 2011-2022 走看看