2.面向对象编程风格

1. 说明

此博客记录如何以面向对象的方式进行编程,以及如何让线程和线程对象同时销毁

2. 相关代码:

2.1 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
2.2 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()
{
   
    //创建一个线程,指定线程入口函数ThreadRoutine,参数为this指针(指向实际对象本身)
    pthread_create(&_threadId, nullptr, ThreadRoutine, this);
}
void Thread::Join()
{
   
    //以阻塞的形式等待指定的线程终止
    pthread_join(_threadId,nullptr);
}

void* Thread::ThreadRoutine(void* arg)
{
   
    //将传递过来的对象指针转换为Thread*(基类)类型
    //即基类指针thread指向了派生类对象
    Thread* thread = static_cast<Thread*>(arg);
    //利用虚函数多态,使用基类指针调用子类对象的函数
    //也可以理解为此时的基类相当于一个库,回调了子类中的虚函数
    thread->Run();
    //当子类对象run函数执行完毕后,自动删除当前线程
    if(thread->_autoDelete){
   
        delete thread;
    }
    return nullptr;
}

void Thread::SetAutoDelete(bool autoDelete)
{
   
    _autoDelete = autoDelete;
}

2.3 Thread.h
#include "Thread.h"
#include <iostream>
#include <unistd.h>

using namespace std;

class TestThread : public Thread
{
   
public:
    TestThread(int count) : _count(count)
    {
   
        cout << "TestThread() ..." << endl;
    }
    ~TestThread()
    {
   
        cout << "~TestThread() ..." << endl;
    }
    void Run()
    {
   
        while (_count--)
        {
   
            cout << "this is a test ..." << endl;
            sleep(1);
        }
        
    }
    int _count;
};

int main()
{
   
    /*
    TestThread t(5);
    t.Start();//隐式的传递了一个参数this(&t --> 指向对象本身)
    t.Join();
    */
    //线程对象和线程本身销毁的时间并不同步
    //使用下述方式实现线程对象和线程本身同时销毁
    TestThread* t2 = new TestThread(5);
    t2->SetAutoDelete(true);
    t2->Start();
    t2->Join();

    cout << "主线程运行结束..." << endl;

    return 0;
}

相关推荐

  1. 2.面向对象编程风格

    2023-12-08 00:04:04       33 阅读
  2. C++面对对象编程进阶(2

    2023-12-08 00:04:04       37 阅读
  3. Python面向对象编程

    2023-12-08 00:04:04       43 阅读
  4. Kotlin——面向对象编程

    2023-12-08 00:04:04       44 阅读
  5. 面向对象编程(一)

    2023-12-08 00:04:04       17 阅读
  6. 面向对象编程(一)

    2023-12-08 00:04:04       19 阅读
  7. 【Python面向对象编程

    2023-12-08 00:04:04       21 阅读
  8. Golang面向对象编程

    2023-12-08 00:04:04       9 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-08 00:04:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-08 00:04:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-08 00:04:04       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-08 00:04:04       20 阅读

热门阅读

  1. 在Python中,*f和**f是用于解包参数的语法

    2023-12-08 00:04:04       41 阅读
  2. 详细学习Pyqt5中的2种弹簧

    2023-12-08 00:04:04       32 阅读
  3. C++this指针与静态成员函数的使用方法

    2023-12-08 00:04:04       40 阅读
  4. 讲一下mysql的锁

    2023-12-08 00:04:04       41 阅读
  5. 【Linux】服务器免密登陆

    2023-12-08 00:04:04       36 阅读
  6. 连接服务器的ssh终端自动断开解放方法

    2023-12-08 00:04:04       37 阅读