当前位置: 首页 > article >正文

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;
}

http://www.kler.cn/news/160826.html

相关文章:

  • 【NLP】如何管理大型语言模型 (LLM)
  • 常规外网打点拿下域控
  • 鸿蒙一出,android开发处境再受重创
  • Kubernetes权威指南:从Docker到Kubernetes实践全接触(第5版)读书笔记 目录
  • screen 常用命令
  • 小程序商城制作一个需要多少钱
  • C++指针作业
  • RT-DETR手把手教程:NEU-DET钢材表面缺陷检测任务 | 不同网络位置加入EMA注意力进行魔改
  • Python-文件详解
  • 烈酒行业分析:预计2029年将达到17628亿元
  • Qt6 QRibbon 一键美化Qt界面
  • Dockerfile与Docker网络
  • 人造草坪市场分析:预计2029年将达到328亿元
  • 基于Java SSM框架实现弹幕视频网站系统项目【项目源码+论文说明】计算机毕业设计
  • 关于一些整理图像及视频数据的代码块
  • 从钓鱼邮件溯源到反制上线
  • 【深度学习】Adversarial Diffusion Distillation,SDXL-Turbo 一步出图
  • 股市复苏中的明懿金汇:抓住新机遇
  • 【C#】使用CancellationToken终止一个正在运行的Task
  • 博捷芯:半导体芯片切割,一道精细工艺的科技之门
  • 从头造轮子 or 重复造轮子?苹果开源 MLX,为自家芯片专属定制机器学习框架
  • 【Java】实现顺序表基本的操作(数据结构)
  • Pytorch CIFAR10图像分类 ShuffleNetv2篇
  • Java数据结构之《构造哈夫曼树》(难度系数85)
  • Java多线程技术二:线程间通信——InheritableThreadLocal的使用
  • BGP/Border Gateway Protocol
  • MySQL系列(一):索引篇
  • Java聊天
  • 浅聊代理(应用部署)
  • 【JavaEE】生产者消费者模式