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);
是因为在Run方法中pthread_create的第三个参数服务
m_pContext = pContext;
int r = pthread_create(&m_ThreadID, 0, StartFunctionOfThread, this);
为什么需要传递this指针?
用来调用特定对象的RunThreadFunction()方法
用来调用特定对象的RunThreadFunction()方法
StartFunctionOfThread是private的,为什么能够被调用?
非静态方法可以调用静态方法
非静态方法可以调用静态方法
能否封装变化点?
一:面向对象的封装
二:基于模板的面向对象的封装
一:面向对象的封装
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;
}
这里用到了基于模板的静态多态。