linux c++ 线程创建的封装

本文介绍了一个C++线程类的封装实现,包括如何使用静态成员函数作为线程启动函数,并通过模板机制实现多态行为,使得线程能够执行不同派生类的具体任务。
class CLThread
{
public:
	CLThread();
	~CLThread();

	CLStatus Run(void *pContext = 0);
	CLStatus WaitForDeath();

private:
	static void* StartFunctionOfThread(void *pContext);

private:
	CLStatus RunThreadFunction();

private:
	void *m_pContext;
	pthread_t m_ThreadID;
};
#include <iostream>
#include "CLThread.h"
#include "CLLog.h"

using namespace std;

CLThread::CLThread()
{
    m_pContext = 0;
}

CLThread::~CLThread()
{
}

CLStatus CLThread::Run(void *pContext)
{    
    m_pContext = pContext;

    int r = pthread_create(&m_ThreadID, 0, StartFunctionOfThread, this);
    if(r != 0)
    {
        CLLog::WriteLogMsg("In CLThread::Run(), pthread_create error", r);
        return CLStatus(-1, 0);
    }

    return CLStatus(0, 0);
}

CLStatus CLThread::WaitForDeath()
{
    int r = pthread_join(m_ThreadID, 0);
    if(r != 0)
    {
        CLLog::WriteLogMsg("In CLThread::WaitForDeath(), pthread_join error", r);
        return CLStatus(-1, 0);
    }

    return CLStatus(0, 0);
}

void* CLThread::StartFunctionOfThread(void *pThis)
{
    CLThread *pThreadThis = (CLThread *)pThis;

    CLStatus s = pThreadThis->RunThreadFunction();

    return (void *)s.m_clReturnCode;
}

CLStatus CLThread::RunThreadFunction()
{
        cout << (int)m_pContext << endl;
    return CLStatus(0, 0);
}

问题:

为什么使用static函数?
是因为在Run方法中pthread_create的第三个参数服务
m_pContext = pContext;
    int r = pthread_create(&m_ThreadID, 0, StartFunctionOfThread, this);
为什么需要传递this指针?
   用来调用特定对象的RunThreadFunction()方法
StartFunctionOfThreadprivate的,为什么能够被调用?
  非静态方法可以调用静态方法
能否封装变化点?
一:面向对象的封装
class CLThread
{
public:
	CLThread();
	virtual ~CLThread();

	CLStatus Run(void *pContext = 0);
	CLStatus WaitForDeath();

private:
	static void* StartFunctionOfThread(void *pContext);

protected:
	virtual CLStatus RunThreadFunction() = 0;

	void *m_pContext;
	pthread_t m_ThreadID;
};
这里讲RunThreadFunction降为子类中实现了。
二:基于模板的面向对象的封装

这里讲RunThreadFunction降为子类中实现了。

template<typename T>
class CLThread
{
public:
	CLThread(){}
	~CLThread(){}

	CLStatus Run(void *pContext = 0)
	{
	    m_pContext = pContext;
	    
	    int r = pthread_create(&m_ThreadID, 0, StartFunctionOfThread, this);
	    if(r != 0)
	    {
		CLLog::WriteLogMsg("In CLThread::Run(), pthread_create error", r);
		return CLStatus(-1, 0);
	    }
	    
	    return CLStatus(0, 0);
	}

	CLStatus WaitForDeath()
	{
	    int r = pthread_join(m_ThreadID, 0);
	    if(r != 0)
	    {
		CLLog::WriteLogMsg("In CLThread::WaitForDeath(), pthread_join error", r);
		return CLStatus(-1, 0);
	    }

	    return CLStatus(0, 0);
	}

private:
	static void* StartFunctionOfThread(void *pContext)
	{

	    T *pThreadThis = (T *)pContext;

	    CLStatus s = pThreadThis->RunThreadFunction();

	    return (void *)s.m_clReturnCode;
	}

	CLStatus RunThreadFunction()
	{
	    return CLStatus(-1, 0);
	}

protected:
	void *m_pContext;
	pthread_t m_ThreadID;
};
调用时

class CLMyThread : public CLThread<CLMyThread>
{
public:
    CLStatus RunThreadFunction()
    {
    int i = (int)m_pContext;
    cout << i << endl;
    }
};

int main()
{
    CLMyThread thread;

    thread.Run((void *)3);
    thread.WaitForDeath();

    return 0;
}

这里用到了基于模板的静态多态。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值