天天看點

一個Windows C++的線程類實作

Thread.h

#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);  

    CThread();  

    CThread(Runnable * pRunnable);  

    CThread(const char * ThreadName, Runnable * pRunnable = NULL);  

    CThread(std::string ThreadName, Runnable * pRunnable = NULL);  

    ~CThread(void);  

    /** 

      開始運作線程 

      @arg bSuspend 開始運作時是否挂起 

    **/  

    bool Start(bool bSuspend = false);  

      運作的線程函數,可以使用派生類重寫此函數 

    virtual void Run();  

      目前執行此函數線程等待線程結束 

      @arg timeout 等待逾時時間,如果為負數,等待無限時長 

    void Join(int timeout = -1);  

      恢複挂起的線程 

    void Resume();  

      挂起線程 

    void Suspend();  

      終止線程的執行 

    bool Terminate(unsigned long ExitCode);  

    unsigned int GetThreadID();  

    std::string GetThreadName();  

    void SetThreadName(std::string ThreadName);  

    void SetThreadName(const char * ThreadName);  

    static unsigned int WINAPI StaticThreadFunc(void * arg);  

    HANDLE m_handle;  

    Runnable * const m_pRunnable;  

    unsigned int m_ThreadID;  

    std::string m_ThreadName;  

    volatile bool m_bRun;  

#endif  

Thread.cpp

#include "Thread.h"  

CThread::CThread(void) :   

m_pRunnable(NULL),  

m_bRun(false)  

}  

CThread::~CThread(void)  

CThread::CThread(Runnable * pRunnable) :   

m_ThreadName(""),  

m_pRunnable(pRunnable),  

CThread::CThread(const char * ThreadName, Runnable * pRunnable) :   

m_ThreadName(ThreadName),  

CThread::CThread(std::string ThreadName, Runnable * pRunnable) :   

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)  

    if(timeout <= 0)  

        timeout = INFINITE;  

    ::WaitForSingleObject(m_handle, timeout);  

void CThread::Resume()  

    ::ResumeThread(m_handle);  

void CThread::Suspend()  

    ::SuspendThread(m_handle);  

bool CThread::Terminate(unsigned long ExitCode)  

    if(::TerminateThread(m_handle, ExitCode))  

        ::CloseHandle(m_handle);  

    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 = "";  

        m_ThreadName = ThreadName;  

unsigned int CThread::StaticThreadFunc(void * arg)  

    CThread * pThread = (CThread *)arg;  

    pThread->Run();  

    return 0;  

用法:

#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();

}