Runnable是个虚基类,任务类和线程类从这里继承,注意virtual的用法
并且添加了一个监听器类
这里总共用到了哪些设计模式呢?
thread.h
#ifndef _THREAD_H
#define _THREAD_H
#include <pthread.h>
#include <iostream>
using namespace std;
class Runnable
{
public:
Runnable();
virtual ~Runnable() {
}
virtual void Run()=0;
void NotifyStop();
void Reset();
bool IsReadyStop();
protected:
volatile bool ready_stop_;
};
class Thread;
//线程事件监听器,当线程创建或退出时,会触发对应的函数调用
class ThreadEventListener
{
public:
virtual ~ThreadEventListener() {
}
virtual void OnThreadCreate(Thread *pThread)=0;
virtual void OnThreadExit(Thread *pThread)=0;
};
class Thread : public Runnable
{
public:
Thread();
explicit Thread(Runnable* pTarget);
void SetTarget(Runnable *pTarget, bool isDeleteTarget = false);
void SetThreadEventListener(ThreadEventListener *pThreadEventListener);
virtual void Run();
bool Start();
virtual void NotifyStop();
void Join(bool isNotifyStop = true);
pthread_t GetTid() {
return tid_;
}
void SetDeleteTarget(bool isDeleteTarget) {
delete_target_ = isDeleteTarget;
}
void FireThreadCreateEvent();
void FireThreadExitEvent();
protected:
Runnable *target_;
ThreadEventListener *thread_event_listener_;
private:
pthread_t tid_;
bool delete_target_;
};
#endif // _THREAD_H
thread.cpp
#include "thread.h"
static void *WorkThread(void *data) {
Thread *thread = (Thread *) data;
if (thread != NULL) {
thread->FireThreadCreateEvent();
thread->Run();
thread->FireThreadExitEvent();
}
return NULL;
}
Runnable::Runnable()
: ready_stop_(false) {
Reset();
}
void Runnable::NotifyStop() {
ready_stop_ = true;
}
void Runnable::Reset() {
ready_stop_ = false;
}
bool Runnable::IsReadyStop() {
return ready_stop_;
}
Thread::Thread()
: target_(NULL),
thread_event_listener_(NULL),
tid_(-1),
delete_target_(false) {
}
Thread::Thread(Runnable *target)
: target_(target),
thread_event_listener_(NULL),
tid_(-1),
delete_target_(false) {
}
void Thread::SetThreadEventListener(
ThreadEventListener *thread_event_listener) {
thread_event_listener_ = thread_event_listener;
}
void Thread::SetTarget(Runnable *target, bool delete_target) {
target_ = target;
delete_target_ = delete_target;
}
void Thread::FireThreadCreateEvent() {
if (thread_event_listener_)
thread_event_listener_->OnThreadCreate(this);
}
void Thread::FireThreadExitEvent() {
if (thread_event_listener_)
thread_event_listener_->OnThreadExit(this);
}
bool Thread::Start() {
if (tid_ != -1) {
return true;
}
pthread_attr_t attr;
pthread_attr_init(&attr);
const int THREAD_STACK_SIZE = 1024 * 1024 * 2;
pthread_attr_setstacksize(&attr, THREAD_STACK_SIZE);
int ret = pthread_create(&(tid_), &attr, WorkThread, (void *) this);
pthread_attr_destroy(&attr);
if (ret != 0) {
return false;
}
return true;
}
void Thread::Run() {
if (target_ != NULL) {
target_->Reset();
target_->Run();
if (delete_target_) {
Runnable *target = target_;
target_ = NULL;
delete target;
}
}
}
void Thread::NotifyStop() {
if (tid_ == -1) {
return ;
}
if (target_ != NULL) {
target_->NotifyStop();
}
}
void Thread::Join(bool isNotifyStop) {
if (isNotifyStop) {
NotifyStop();
}
if (tid_ != -1) {
pthread_join(tid_, NULL);
}
tid_ = -1;
}
class Task : public Runnable
{
public:
void Run()
{
for (int i = 0; i < 10; i ++)
{
cout << "run..." << endl;
}
}
};
int main()
{
Task task;
Thread t1(&task);
t1.Start();
t1.Join();
return 0;
}
编译:g++ -o thread thread.cpp -lpthread
网友评论