Thread.h
[CPP]View PlainCopy
- #ifndef __thread_h__
- #define __thread_h__
- #include <string>
- #include <windows.h>
- #include <process.h>
- Class Runnable
- {
- Public
- Virtual ~runnable () {};
- virtual void Run () = 0;
- };
- Class Cthread: Public Runnable
- {
- Private
- explicit Cthread (const cthread & RHS);
- Public
- Cthread ();
- Cthread (Runnable * prunnable);
- Cthread (const char * threadname, Runnable * prunnable = NULL);
- Cthread (std::string threadname, Runnable * prunnable = NULL);
- ~cthread (void);
- /**
- Start Running Threads
- @arg whether Bsuspend hangs when it starts running
- **/
- bool Start (bool bsuspend = false);
- /**
- Run a thread function that can be overridden with a derived class
- **/
- virtual void Run ();
- /**
- Currently executing this function thread waits for thread to end
- @arg Timeout Wait timeout, if negative, wait for unlimited duration
- **/
- void Join (int timeout =-1);
- /**
- Recovering a suspended thread
- **/
- void Resume ();
- /**
- Suspending threads
- **/
- void Suspend ();
- /**
- Terminating the execution of a thread
- **/
- bool Terminate (unsigned long ExitCode);
- unsigned int getthreadid ();
- std::string getthreadname ();
- void SetThreadName (std::string threadname);
- void SetThreadName (const char * threadname);
- Private
- static unsigned int WINAPI staticthreadfunc (void * arg);
- Private
- HANDLE M_handle;
- Runnable * const m_prunnable;
- unsigned int m_threadid;
- Std::string M_threadname;
- volatile bool M_brun;
- };
- #endif
Thread.cpp
[CPP]View PlainCopy
- #include "Thread.h"
- Cthread::cthread (void):
- M_prunnable (NULL),
- M_brun (false)
- {
- }
- Cthread::~cthread (void)
- {
- }
- Cthread::cthread (Runnable * prunnable):
- M_threadname (""),
- M_prunnable (prunnable),
- M_brun (false)
- {
- }
- Cthread::cthread (const char * threadname, Runnable * prunnable):
- M_threadname (ThreadName),
- M_prunnable (prunnable),
- M_brun (false)
- {
- }
- Cthread::cthread (std::string threadname, Runnable * prunnable):
- M_threadname (ThreadName),
- M_prunnable (prunnable),
- M_brun (false)
- {
- }
- BOOL Cthread::start (bool bsuspend)
- {
- if (m_brun)
- {
- return true;
- }
- if (bsuspend)
- {
- M_handle = (handle) _beginthreadex (NULL, 0, Staticthreadfunc, this , create_suspended, &m_threadid);
- }
- Else
- {
- M_handle = (handle) _beginthreadex (NULL, 0, Staticthreadfunc, this , 0, &m_threadid);
- }
- M_brun = (NULL! = M_handle);
- return m_brun;
- }
- void Cthread::run ()
- {
- if (!m_brun)
- {
- return;
- }
- if (NULL! = m_prunnable)
- {
- M_prunnable->run ();
- }
- M_brun = false;
- }
- void Cthread::join (int timeout)
- {
- if (NULL = = M_handle | |!m_brun)
- {
- return;
- }
- if (timeout <= 0)
- {
- Timeout = INFINITE;
- }
- :: WaitForSingleObject (M_handle, timeout);
- }
- void Cthread::resume ()
- {
- if (NULL = = M_handle | |!m_brun)
- {
- return;
- }
- :: ResumeThread (M_handle);
- }
- void Cthread::suspend ()
- {
- if (NULL = = M_handle | |!m_brun)
- {
- return;
- }
- :: SuspendThread (M_handle);
- }
- BOOL Cthread::terminate (unsigned long ExitCode)
- {
- if (NULL = = M_handle | |!m_brun)
- {
- return true;
- }
- if (:: TerminateThread (M_handle, ExitCode))
- {
- :: CloseHandle (M_handle);
- return true;
- }
- return false;
- }
- unsigned int cthread::getthreadid ()
- {
- return m_threadid;
- }
- std::string Cthread::getthreadname ()
- {
- return m_threadname;
- }
- void Cthread::setthreadname (std::string threadname)
- {
- M_threadname = ThreadName;
- }
- void Cthread::setthreadname (const char * threadname)
- {
- if (NULL = = threadname)
- {
- M_threadname = "";
- }
- Else
- {
- M_threadname = ThreadName;
- }
- }
- unsigned int cthread::staticthreadfunc (void * arg)
- {
- Cthread * PThread = (Cthread *) arg;
- Pthread->run ();
- return 0;
- }
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)