A Windows C + + threading class implementation (encapsulating the API to form a class, but not perfect.) In fact, you can learn the TThread of Delphi)

Source: Internet
Author: User

Thread.h

[CPP]View PlainCopy
  1. #ifndef __thread_h__
  2. #define __thread_h__
  3. #include <string>
  4. #include <windows.h>
  5. #include <process.h>
  6. Class Runnable
  7. {
  8. Public
  9. Virtual ~runnable () {};
  10. virtual void Run () = 0;
  11. };
  12. Class Cthread: Public Runnable
  13. {
  14. Private
  15. explicit Cthread (const cthread & RHS);
  16. Public
  17. Cthread ();
  18. Cthread (Runnable * prunnable);
  19. Cthread (const char * threadname, Runnable * prunnable = NULL);
  20. Cthread (std::string threadname, Runnable * prunnable = NULL);
  21. ~cthread (void);
  22. /** 
  23. Start Running Threads
  24. @arg whether Bsuspend hangs when it starts running
  25. **/
  26. bool Start (bool bsuspend = false);
  27. /** 
  28. Run a thread function that can be overridden with a derived class
  29. **/
  30. virtual void Run ();
  31. /** 
  32. Currently executing this function thread waits for thread to end
  33. @arg Timeout Wait timeout, if negative, wait for unlimited duration
  34. **/
  35. void Join (int timeout =-1);
  36. /** 
  37. Recovering a suspended thread
  38. **/
  39. void Resume ();
  40. /** 
  41. Suspending threads
  42. **/
  43. void Suspend ();
  44. /** 
  45. Terminating the execution of a thread
  46. **/
  47. bool Terminate (unsigned long ExitCode);
  48. unsigned int getthreadid ();
  49. std::string getthreadname ();
  50. void SetThreadName (std::string threadname);
  51. void SetThreadName (const char * threadname);
  52. Private
  53. static unsigned int WINAPI staticthreadfunc (void * arg);
  54. Private
  55. HANDLE M_handle;
  56. Runnable * const m_prunnable;
  57. unsigned int m_threadid;
  58. Std::string M_threadname;
  59. volatile bool M_brun;
  60. };
  61. #endif

Thread.cpp

[CPP]View PlainCopy
  1. #include "Thread.h"
  2. Cthread::cthread (void):
  3. M_prunnable (NULL),
  4. M_brun (false)
  5. {
  6. }
  7. Cthread::~cthread (void)
  8. {
  9. }
  10. Cthread::cthread (Runnable * prunnable):
  11. M_threadname (""),
  12. M_prunnable (prunnable),
  13. M_brun (false)
  14. {
  15. }
  16. Cthread::cthread (const char * threadname, Runnable * prunnable):
  17. M_threadname (ThreadName),
  18. M_prunnable (prunnable),
  19. M_brun (false)
  20. {
  21. }
  22. Cthread::cthread (std::string threadname, Runnable * prunnable):
  23. M_threadname (ThreadName),
  24. M_prunnable (prunnable),
  25. M_brun (false)
  26. {
  27. }
  28. BOOL Cthread::start (bool bsuspend)
  29. {
  30. if (m_brun)
  31. {
  32. return true;
  33. }
  34. if (bsuspend)
  35. {
  36. M_handle = (handle) _beginthreadex (NULL, 0, Staticthreadfunc, this , create_suspended, &m_threadid);
  37. }
  38. Else
  39. {
  40. M_handle = (handle) _beginthreadex (NULL, 0, Staticthreadfunc, this , 0, &m_threadid);
  41. }
  42. M_brun = (NULL! = M_handle);
  43. return m_brun;
  44. }
  45. void Cthread::run ()
  46. {
  47. if (!m_brun)
  48. {
  49. return;
  50. }
  51. if (NULL! = m_prunnable)
  52. {
  53. M_prunnable->run ();
  54. }
  55. M_brun = false;
  56. }
  57. void Cthread::join (int timeout)
  58. {
  59. if (NULL = = M_handle | |!m_brun)
  60. {
  61. return;
  62. }
  63. if (timeout <= 0)
  64. {
  65. Timeout = INFINITE;
  66. }
  67. :: WaitForSingleObject (M_handle, timeout);
  68. }
  69. void Cthread::resume ()
  70. {
  71. if (NULL = = M_handle | |!m_brun)
  72. {
  73. return;
  74. }
  75. :: ResumeThread (M_handle);
  76. }
  77. void Cthread::suspend ()
  78. {
  79. if (NULL = = M_handle | |!m_brun)
  80. {
  81. return;
  82. }
  83. :: SuspendThread (M_handle);
  84. }
  85. BOOL Cthread::terminate (unsigned long ExitCode)
  86. {
  87. if (NULL = = M_handle | |!m_brun)
  88. {
  89. return true;
  90. }
  91. if (:: TerminateThread (M_handle, ExitCode))
  92. {
  93. :: CloseHandle (M_handle);
  94. return true;
  95. }
  96. return false;
  97. }
  98. unsigned int cthread::getthreadid ()
  99. {
  100. return m_threadid;
  101. }
  102. std::string Cthread::getthreadname ()
  103. {
  104. return m_threadname;
  105. }
  106. void Cthread::setthreadname (std::string threadname)
  107. {
  108. M_threadname = ThreadName;
  109. }
  110. void Cthread::setthreadname (const char * threadname)
  111. {
  112. if (NULL = = threadname)
  113. {
  114. M_threadname = "";
  115. }
  116. Else
  117. {
  118. M_threadname = ThreadName;
  119. }
  120. }
  121. unsigned int cthread::staticthreadfunc (void * arg)
  122. {
  123. Cthread * PThread = (Cthread *) arg;
  124. Pthread->run ();
  125. return 0;
  126. }

Usage:

#include "Thread.h"
#include "ThreadPoolExecutor.h"

Class R:public Runnable
{
Public
~r ()
{
printf ("~r/n");
}
void Run ()
{
printf ("Hello world/n");
}
};

int _tmain (int argc, _tchar* argv[])
{
R R;
Cthread * t = NULL;
t = new Cthread (&R);
T->start ();
T->join ();

GetChar ();

}

http://blog.csdn.net/huyiyang2010/article/details/5801597

A Windows C + + threading class implementation (encapsulating the API to form a class, but not perfect.) In fact, you can learn the TThread of Delphi)

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.