Thread类的封装
用面向对象的方式封装一个Thread类
实现如下:
Thread.h
#ifndef _THREAD_H_
#define _THREAD_H_
#include <pthread.h>
class Thread
{
public:
Thread();
virtual ~Thread();
void Start();
void Join();
void SetAutoDelete(bool autoDelete);
private:
static void* ThreadRoutine(void* arg);
virtual void Run() = 0;
pthread_t threadId_;
bool autoDelete_;
};
#endif // _THREAD_H_
Thread.cpp:
#include "Thread.h"
#include <iostream>
using namespace std;
Thread::Thread() : autoDelete_(false)
{
cout<<"Thread ..."<<endl;
}
Thread::~Thread()
{
cout<<"~Thread ..."<<endl;
}
void Thread::Start()
{
pthread_create(&threadId_, NULL, ThreadRoutine, this);
}
void Thread::Join()
{
pthread_join(threadId_, NULL);
}
void* Thread::ThreadRoutine(void* arg)
{
Thread* thread = static_cast<Thread*>(arg);
thread->Run();
if (thread->autoDelete_)
delete thread;
return NULL;
}
void Thread::SetAutoDelete(bool autoDelete)
{
autoDelete_ = autoDelete;
}
该基类提供了Start函数来开启一个线程,线程的回调函数为ThradRuntine,ThreadRuntine设置为私有函数,这里将该函数设置为静态的。为什么设置为静态的是有原因的,理论上Run函数才是线程回调该调用的函数,然而Run函数是需要子类来覆写实现自己的业务逻辑,也为此Run函数使用了virtual关键字。Run函数是Thread基类的一个普通的成员函数,所以其实Run函数本质上是void Run(this)的,在形参中隐藏了this指针,然而pthread_create函数需要的是一个普通的函数,函数定义如下: void (start_runtine)(void*)。所以才需要将ThreadRuntine定义为全局或者静态的,定义为全局将将该函数全部暴露,所以定义为静态。
在ThreadRoutine中运行实际业务的Run函数,这里需要注意的是pthead_create函数传递了this指针给ThreadRuntine函数,因为该函数是静态的,它不能操作非静态的函数或者变量,所以在该函数中通过传入的this指针来调用Run函数,然后在调用完毕之后delete掉this,自动销毁自身对象。
test:
#include "Thread.h"
#include <unistd.h>
#include <iostream>
using namespace std;
class TestThread : public Thread
{
public:
TestThread(int count) : count_(count)
{
cout<<"TestThread ..."<<endl;
}
~TestThread()
{
cout<<"~TestThread ..."<<endl;
}
private:
void Run()
{
while (count_--)
{
cout<<"this is a test ..."<<endl;
sleep(1);
}
}
int count_;
};
int main(void)
{
TestThread* t2 = new TestThread(5);
t2->SetAutoDelete(true);
t2->Start();
t2->Join();
for (; ; )
pause();
return 0;
}
基于对象风格的Thread类封装
使用C++11的function和bind来实现基于对象风格的Thread类封装
#include <iostream>
#include<functional>
using namespace std;
class Foo
{
public:
void memberFunc(double d, int i, int j)
{
cout << d << endl;//打印0.5
cout << i << endl;//打印100
cout << j << endl;//打印10
}
};
int main()
{
Foo foo;
std::function<void (int, int)> fp = std::bind(&Foo::memberFunc, &foo, 0.5, std::placeholders::_1,std::placeholders::_2);
fp(100, 200);
std::function<void (int, int)> fp2 = std::bind(&Foo::memberFunc, std::ref(foo), 0.5, std::placeholders::_1,std::placeholders:: _2);
fp2(55, 66);
return 0;
}
Thread.h:
#ifndef _THREAD_H_
#define _THREAD_H_
#include <pthread.h>
#include<iostream>
#include<functional>
class Thread
{
public:
typedef std::function<void ()> ThreadFunc;
explicit Thread(const ThreadFunc& func);
void Start();
void Join();
void SetAutoDelete(bool autoDelete);
private:
static void* ThreadRoutine(void* arg);
void Run();
ThreadFunc func_;
pthread_t threadId_;
bool autoDelete_;
};
#endif // _THREAD_H_
Thread.cpp
#include "Thread.h"
#include <iostream>
using namespace std;
Thread::Thread(const ThreadFunc& func) : func_(func), autoDelete_(false)
{
}
void Thread::Start()
{
pthread_create(&threadId_, NULL, ThreadRoutine, this);
}
void Thread::Join()
{
pthread_join(threadId_, NULL);
}
void* Thread::ThreadRoutine(void* arg)
{
Thread* thread = static_cast<Thread*>(arg);
thread->Run();
if (thread->autoDelete_)
delete thread;
return NULL;
}
void Thread::SetAutoDelete(bool autoDelete)
{
autoDelete_ = autoDelete;
}
void Thread::Run()
{
func_();
}
test:
#include "Thread.h"
#include <boost/bind.hpp>
#include <unistd.h>
#include <iostream>
using namespace std;
class Foo
{
public:
Foo(int count) : count_(count)
{
}
void MemberFun()
{
while (count_--)
{
cout<<"this is a test ..."<<endl;
sleep(1);
}
}
void MemberFun2(int x)
{
while (count_--)
{
cout<<"x="<<x<<" this is a test2 ..."<<endl;
sleep(1);
}
}
int count_;
};
void ThreadFunc()
{
cout<<"ThreadFunc ..."<<endl;
}
void ThreadFunc2(int count)
{
while (count--)
{
cout<<"ThreadFunc2 ..."<<endl;
sleep(1);
}
}
int main(void)
{
Thread t1(ThreadFunc);
Thread t2(boost::bind(ThreadFunc2, 3));
Foo foo(3);
Thread t3(boost::bind(&Foo::MemberFun, &foo));
Foo foo2(3);
Thread t4(boost::bind(&Foo::MemberFun2, &foo2, 1000));
t1.Start();
t2.Start();
t3.Start();
t4.Start();
t1.Join();
t2.Join();
t3.Join();
t4.Join();
return 0;
}
在测试程序中先创建了一个线程类t1,t1调用ThreadFunc函数打印了一次信息;
然后程序创建了t2线程,传入函数绑定了ThreadFunc2,其中传入了参数3,然后执行三次打印信息;
在主程序中创建了Foo对象,然后分别绑定该Foo对象的两个函数,打印相关信息
总结
- C编程风格:注册全局函数到网络库,然后通过函数指针进行回调
- 面向对象风格:用一个类继承一个抽象类,实现接口的具体业务逻辑
- 基于对象风格:用一个类包含一个具体类对象,在构造函数中用bind来注册成员函数