thread类

作者: 老练子丶2017 | 来源:发表于2017-11-16 17:20 被阅读0次

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

相关文章

  • Android多线程使用详解

    一、Thread、HandlerThread MyThread继承于Thread类,重载Thread类的run函数...

  • Thread类详解以及创建线程的四种方法

    1.Thread 类详解 Thread类是Java语言中重要的基础类,位于java.lang包中。Thread类有...

  • 多线程创建的两种基本方法

    方法1 继承Thread() 继承Thread()类的多线程创建步骤 新建一个类继承 Thread() 类 重写 ...

  • 2018-09-03 java多线程

    1.extends Thread类定义:Thread thread=new myThread();thread....

  • thread类

    Runnable是个虚基类,任务类和线程类从这里继承,注意virtual的用法 并且添加了一个监听器类 这里总共用...

  • 线程创建方式

    继承Thread类: 步骤:①、定义类继承Thread;②、复写Thread类中的run方法;目的:将自定义代码存...

  • 2020-10-25 Java线程

    Thread类java中一个类可以通过继承Thread类实现run()实现多线程。继承thread的类通过star...

  • Thread、Runnable、Callable

    1.Thread/** 继承Thread类创建线程类 (1)定义Thread类的子类,重写该类的run方法,该ru...

  • 基本线程类

    一、Thread 类 Thread类实现了Runnable接口,在Thread类中,有一些比较关键的属性 Thre...

  • Java中实现多线程的方式

    [TOC] 一、继承Thread类 继承Thread类,重写run方法,调用Thread的start()方法启动线...

网友评论

    本文标题:thread类

    本文链接:https://www.haomeiwen.com/subject/qzfrvxtx.html