Runnable是個(gè)虛基類迁客,任務(wù)類和線程類從這里繼承,注意virtual的用法
并且添加了一個(gè)監(jiān)聽(tīng)器類
這里總共用到了哪些設(shè)計(jì)模式呢柜去?
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;
//線程事件監(jiān)聽(tīng)器,當(dāng)線程創(chuàng)建或退出時(shí)挟裂,會(huì)觸發(fā)對(duì)應(yīng)的函數(shù)調(diào)用
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