Thread encapsulation classes for cross-platform (Win and Unix)

Source: Internet
Author: User
Tags win32

  1. #ifdef WIN32
  2. #include <Windows.h>
  3. #include <process.h>
  4. #else
  5. #include <pthread.h>
  6. #endif
  7. /*
  8. #ifdef WIN32
  9. typedef unsigned INT (__stdcall *thread_func) (void*);
  10. #else
  11. typedef void* (*thread_func) (void*);
  12. #endif
  13. */
  14. Class Base_thread
  15. {
  16. Public
  17. Base_thread ();
  18. virtual ~base_thread ();
  19. bool Create ();
  20. void Wait ();
  21. virtual void run () = 0;
  22. #ifdef WIN32
  23. static unsigned __stdcall thread_func (void* Arg);
  24. #else
  25. static void* thread_func (void* Arg);
  26. #endif
  27. Protected
  28. #ifdef WIN32
  29. HANDLE M_handle;
  30. #else
  31. pthread_t m_thread_t;
  32. #endif
  33. };
  34. #endif
[CPP]View PlainCopy
  1. Base_thread::base_thread ()
  2. {
  3. #ifdef WIN32
  4. M_handle = NULL;
  5. #else
  6. m_thread_t = 0;
  7. #endif
  8. }
  9. Base_thread::~base_thread ()
  10. {
  11. #ifdef WIN32
  12. if (NULL! = m_handle)
  13. {
  14. CloseHandle (M_handle);
  15. }
  16. M_handle = NULL;
  17. #else
  18. m_thread_t = 0;
  19. #endif
  20. }
  21. BOOL Base_thread::create ()
  22. {
  23. BOOL ret = false;
  24. #ifdef WIN32
  25. M_handle = (handle) _beginthreadex (null, 0, Thread_func, this , 0, NULL);
  26. if (NULL! = m_handle)
  27. {
  28. ret = true;
  29. }
  30. #else
  31. if (0 = = Pthread_create (&m_thread_t, NULL, Thread_func, this ))
  32. {
  33. ret = true;
  34. }
  35. Else
  36. {
  37. m_thread_t = 0;
  38. }
  39. #endif
  40. return ret;
  41. }
  42. void Base_thread::wait ()
  43. {
  44. #ifdef WIN32
  45. WaitForSingleObject (M_handle, INFINITE);
  46. if (NULL! = m_handle)
  47. {
  48. CloseHandle (M_handle);
  49. }
  50. M_handle = NULL;
  51. #else
  52. Pthread_join (m_thread_t, NULL);
  53. m_thread_t = 0;
  54. #endif//WIN32
  55. }
  56. #ifdef WIN32
  57. unsigned __stdcall base_thread::thread_func (void* Arg)
  58. #else
  59. void* Base_thread::thread_func (void* Arg)
  60. #endif
  61. {
  62. Base_thread *pthis = (base_thread*) arg;
  63. Pthis->run ();
  64. return NULL;
  65. }

Encapsulates a line base class that can be used under Windows and Linux, where the Run method requires the inherited subclass to be implemented, which is equivalent to the thread function, as you can see, in the base class Base_thread, I call the method run in the thread function. Wait is used to wait for a thread-safe exit to stay stuck in the main thread.

Thread encapsulation classes for cross-platform (Win and Unix)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.