C++基础-多线程扩展继承(public:thread)


对thread进行继承,这里overite thread的构造方法 

 Huathread():thread(){ //子类调用父类的方法

    };
    template<typename T, typename...Args> //子类调用父类的构造函数, 可变参数的构造
    Huathread(T && func, Args &&...args):thread(std::forward<T>(func),forward<Args>(args)...)
    {

    }

定义我们自己的方法

  void run(const char* cmd) //新增功能
    {
        system(cmd);
    }

完整代码

//
// Created by Administrator on 2021/6/27.
//
#include<thread>
#include<iostream>

using namespace std;

class Huathread : public thread{
public:
    Huathread():thread(){ //子类调用父类的方法

    };
    template<typename T, typename...Args> //子类调用父类的构造函数, 可变参数的构造
    Huathread(T && func, Args &&...args):thread(std::forward<T>(func),forward<Args>(args)...)
    {

    }

    void run(const char* cmd) //新增功能
    {
        system(cmd);
    }

};
int main()
{
    Huathread t1([](){
        cout << "hello this is huahua" << endl;
    });
    t1.run("calc");

    Huathread t2([](int num){
        cout << "hello this is huahua" << num << endl;
    }, 100);
    t1.run("notepad");
    cin.get();
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM