面向对象和基于对象
面向对象和基于对象封装Thread类
一、面向对象封装
Thread类图
//Thread.h
class Thread
{
public:
Thread();
virtual ~Thread();
public:
void Start(); //线程创建
void Join(); //回收线程资源
void SetAutoDelete(bool autoDel);//设置线程对象的执行完后回收
private:
virtual void Run() = 0; //纯虚函数,子类实现Run() 方法
static void *ThreadRoutine(void *args); //线程的入口函数
private:
pthread_t threadId_; //线程ID
bool autoDelete_; //回收方式
};
//Thread.cpp
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 *args)
{
Thread *thread = static_cast<Thread *>(args);//args为子类this指针,父类指针指向子类对象实现回调
thread->Run(); //子类的 Run() 方法
if (thread->autoDelete_)
{
delete thread;
}
return NULL;
}
void Thread::SetAutoDelete(bool autoDel)
{
autoDelete_ = autoDel;
}
//ThreadTest.cpp
class TestThread : public Thread //继承Thread类,重写Run方法,通过虚函数实现回调
{
public:
TestThread(int count) : count_(count)
{
cout << "TestThread()" << endl;
}
~TestThread()
{
cout << "~TestThread()" << endl;
}
private:
void Run() //线程具体的执行函数
{
while (count_--)
{
cout << "This is a thread test!!!" << endl;
sleep(1);
}
}
private:
int count_;
};
int main()
{
TestThread *t2 = new TestThread(5);
t2->SetAutoDelete(true);
t2->Start();
t2->Join();
return 0;
}
二、基于对象封装
Thread类图
基于对象通过boost::function, boost::bind实现函数回调
class Thread
{
public:
typedef boost::function<void()> ThreadFunc; //函数对象类型,使用boost::function, C++11也可以
public:
explicit Thread(const ThreadFunc &func); //指定使用此构造函数
~Thread();
public:
void Start(); // 线程创建
void Join(); // 线程回收
void SetAutoDelete(bool autoDel);
private:
void Run(); // 线程运行函数
static void *ThreadRoutine(void *args); //线程入口函数
private:
ThreadFunc func_; //函数对象
pthread_t threadId_; //线程ID
bool autoDelete_;
};
//Thread.cpp
Thread::Thread(const ThreadFunc &func) : func_(func), 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 *args)
{
Thread *thread = static_cast<Thread *>(args);
thread->Run();
if (thread->autoDelete_)
{
delete thread;
}
return NULL;
}
void Thread::SetAutoDelete(bool autoDel)
{
autoDelete_ = autoDel;
}
void Thread::Run()
{
func_();
}
//ThreadTest.cpp
class Foo //测试用类
{
public:
Foo(int count) : count_(count)
{
}
void MemberFunc()
{
while (count_--)
{
cout << "This is a Class MemberFunc Test!!!" << endl;
sleep(1);
}
}
void MemberFunc2(int x)
{
while (count_--)
{
cout << "x = " << x << " This is Class MemberFunc(x) Test!!!" << endl;
sleep(1);
}
}
public:
int count_;
};
void ThreadFunc()
{
cout << "ThreadFunc" << endl;
}
void ThreadFunc2(int count)
{
cout << "ThreadFunc" << endl;
while (count--)
{
cout << count << endl;
sleep(1);
}
}
int main()
{
/*用boost::function函数对象, boost::bind 实现函数回调*/
Foo foo(5);
Foo foo2(3);
Thread t1(ThreadFunc); //普通函数作为线程的执行函数
Thread t2(boost::bind(ThreadFunc2, 5)); //用boost::bind将有参函数转换为无参函数
//类的成员函数作为线程的执行函数,用boost::bind
Thread t3(boost::bind(&Foo::MemberFunc, &foo));
Thread t4(boost::bind(&Foo::MemberFunc2, &foo2, 5)); //用boost::bind将有参函数转换为无参函数
t1.Start();
t2.Start();
t3.Start();
t4.Start();
t1.Join();
t2.Join();
t3.Join();
t4.Join();
return 0;
}