用C++模板封装线程

 提供一种封装线程的思路。
   开发服务器程序,最常用的技术是多线程技术,有很多种线程封装重用的方法,利用C++模板特性封装线程,
能够在开发的时候更多的关注线程的业务,无须关心线程的创建,并且不同的平台,创建线程的方式都很单一。

代码如下:

// 线程业务抽象类,具体业务继承该类,实现接口即可

class CThreadExecuter
{
public:
    CThreadExecuter(void) {}
    virtual ~CThreadExecuter(void) {}

public:

 // 返回值:线程执行错误码
 // iPara -- 业务参数,可为其他对象或标识

 int Execute(int iPara = 0)
    {
        return ExecuteImp(iPara);
    }

    // 可根据需要重载函数声明

    virtual void Stop(void) {}

    // 可用于执行特定工程定义的任务

    virtual void AddCommand(int iCmdID, void* pPara) {}

protected:

    virtual int ExecuteImp(int iPara) = 0;

};

//
const int THREAD_EXECUTE_SLEEP_TIME_DEFAULT = 10;    // thread default sleep time
const int THREAD_STOP_WAIT_TIME_DEFAULT = 2 * 1000;  // thread wait time default value that stop thread

//
// 这是线程的基类,通过将线程控制和执行业务分离分别独立演化,达到线程重用的目的
// 线程基类为模板类,使用时必须先实现具体的事务执行类,可从CThreadExecuter类继承
// 或者实现了Execute(int iSleepTime), Stop接口的任意类
template <class TExecuter>
class CThread
{
public:
    CThread(void);
    virtual ~CThread(void);

public:
    // sleep time is milliseconds
    int Start(int iPara, int iSleepTime = THREAD_EXECUTE_SLEEP_TIME_DEFAULT, bool bSuspended = false);
    // wait time is milliseconds
    int Stop(int iWaitTime = THREAD_STOP_WAIT_TIME_DEFAULT)
    {
        return StopImp(iWaitTime);
    }
    // suspends thread
    int Suspend(void)
    {
        return SuspendImp();
    }
    // decrements thread's suspend count.
    // When the suspend count is decremented to zero, the execution of the thread is resumed
    int Resume(void)
    {
        return ResumeImp();
    }
    bool IsRunning(void)
    {
        return IsRunningImp();
    }
   
    //
    int GetThreadID(void) const
    {
        return m_iThreadID;
    }

    TExecuter* GetExecuter(void)
    {
        return &m_objExecuter;
    }

public:
    // 该接口子类可实现,也可不实现,并且允许重载
    virtual int SetPriority(int iPriority)
    {
        return 0;
    }

protected:
    // derive class must implement method declare
    virtual int StartImp(bool bSuspend) = 0;
    virtual int StopImp(int iWaitTime) = 0;
    virtual int SuspendImp(void) = 0;
    virtual int ResumeImp(void) = 0;
    virtual bool IsRunningImp(void) = 0;

    // 线程执行对象函数,由子类具体实现
    virtual int Execute(void) = 0;

protected:
    int m_iSleepTime;  // thread execute sleep time, is milliseconds
    int m_iThrdPara;   //

    int m_iThreadID;   // thread ID

    // 具体的线程对象句柄根据所使用的OS决定
    // 具体的线程执行函数也要根据OS,线程创建API不同决定

    TExecuter m_objExecuter;       // 线程执行者对象
};

template <class TExecuter>
CThread<TExecuter>::CThread(void) : m_iSleepTime(0), m_iThrdPara(0), m_iThreadID(0)
{
}


template <class TExecuter>
CThread<TExecuter>::~CThread(void)
{
}


template <class TExecuter>
int CThread<TExecuter>::Start(int iPara, int iSleepTime /* = THREAD_EXECUTE_SLEEP_TIME_DEFAULT */, bool bSuspended /* = false */)
{
    m_iSleepTime = iSleepTime;
    m_iThrdPara = iPara;
    return StartImp(bSuspended);
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值