C++ 线程(二)

#include <process.h>
#include <iostream>
#include <windows.h>
#include <string> 
using namespace  std;
 
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); 
 
 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); 
 
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; 
}; 

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) 

 cout << ThreadName << endl;

 
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; 


class Test : public Runnable
{
public:
    ~Test()
    {
        printf("~test \n");
    }
    void Run()
    {
        printf("richard Hello World\n");
    }
};

int main(int argc, char *argv[])
{
    Test m;
    CThread * t = NULL;
    t = new CThread("cthread test",&m);
    t->Start();
    t->Join();
    return 0;
}


 

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值