C++ Thread多线程并发记录(3)线程创建总结

1.启动线程传递全局函数

cpp 复制代码
#include <iostream>
#include <thread>

void Th1(int id){
    std::cout << "Create global fun. id = " << id << std::endl;
}
void TH1(const int &id){
    std::cout << "Create global fun. id = " << id << std::endl;
}

int main() {
    std::thread th1(Th1, 1);
    const int &num = 2;
    std::thread th2(Th1,std::ref(num));
    th1.join();//等待子线程结束后汇聚,注意同一个thread对象join两次会产生未定义行为
    th2.join();
    //th2.detach();//detach方法将线程变为守护进程,有可能出现主线程结束后,子线程还未结束的情况,故而不要使用detach线程访问栈内存
    return 0;
}

2.启动线程传递类成员函数

cpp 复制代码
#include <iostream>
#include <string>
#include <thread>

class TH1{
public:
    void print(){
        std::cout << name << std::endl;
    }
    std::string name{};
};
class TH2{
public:
    void operator()(std::string name) {
        std::cout << name << std::endl;
    }
};

int main() {
    TH1 t1;
    t1.name = "this is thread 1.";
    std::thread th1(&TH1::print, &t1);//传递类成员函数地址以及类实例地址
    std::thread th2((TH2()), "this is thread 2.");

    th1.join();
    th2.join();
    return 0;
}

3.传递Lambda表达式

cpp 复制代码
#include <iostream>
#include <string>
#include <thread>

int main() {
    std::string name{"This is thread 1."};
    std::thread t1([](const std::string &s){ std::cout << s << std::endl; }, std::ref(name));
    t1.join();
    return 0;
}

4.Thread RAII管理线程

cpp 复制代码
#include <iostream>
#include <string>
#include <memory>
#include <vector>
#include <thread>

class Thread_join {
public:
    Thread_join() = default;
    virtual ~Thread_join() = default;

    virtual void start() {
        thread_ = std::thread(&Thread_join::do_something, this); //传入基类指针产生动态绑定,根据动态类型选择虚函数类型
    };

    virtual void wait(){
        if (thread_.joinable())
            thread_.join();
    }
protected:
    std::thread thread_; //使用智能指针管理thread需要自定义
    virtual void do_something() = 0; //类中方法声明为纯虚函数
};

class TH1 : public Thread_join {
public:
    void do_something() override {
        for (const auto &i : s)
            std::cout << i << " ";
    }
private:
    std::vector<std::string> s{"This", "is", "thread", "RAII"};
};

int main() {
    TH1 t;
    t.start();
    t.wait();

    return 0;
}
cpp 复制代码
#include <iostream>
#include <string>
#include <vector>
#include <thread>

class Thread_join {
public:
    Thread_join() = default;
    virtual ~Thread_join(){
        if (thread_.joinable())
            thread_.join();
    }

    virtual void start() {
        thread_ = std::thread(&Thread_join::do_something, this); //传入基类指针产生动态绑定,根据动态类型选择虚函数类型
    };

    virtual void wait(){
        if (thread_.joinable())
            thread_.join();
    }
protected:
    std::thread thread_; //使用智能指针管理thread需要自定义
    virtual void do_something() = 0; //类中方法声明为纯虚函数
};

class TH1 : public Thread_join {
public:
    ~TH1() override{
        if (thread_.joinable())
            thread_.join();
    }
    void do_something() override {
        for (const auto &i : s)
            std::cout << i << " ";
    }
private:
    std::vector<std::string> s{"This", "is", "thread", "RAII"};
};

int main() {
    TH1 t;
    t.start();

    return 0;
}
相关推荐
5:0021 分钟前
云备份项目
linux·开发语言·c++
乄夜1 小时前
嵌入式面试高频(5)!!!C++语言(嵌入式八股文,嵌入式面经)
c语言·c++·单片机·嵌入式硬件·物联网·面试·职场和发展
YYDS3141 小时前
C++动态规划-01背包
开发语言·c++·动态规划
wydaicls2 小时前
十一.C++ 类 -- 面向对象思想
开发语言·c++
姜君竹2 小时前
QT的工程文件.pro文件
开发语言·c++·qt·系统架构
思捻如枫2 小时前
C++数据结构和算法代码模板总结——算法部分
数据结构·c++
weixin_478689763 小时前
C++ 对 C 的兼容性
java·c语言·c++
k要开心3 小时前
C++概念以及基础框架语法
开发语言·c++
weixin_307779134 小时前
Linux下GCC和C++实现统计Clickhouse数据仓库指定表中各字段的空值、空字符串或零值比例
linux·运维·c++·数据仓库·clickhouse
秦少游在淮海5 小时前
C++ - string 的使用 #auto #范围for #访问及遍历操作 #容量操作 #修改操作 #其他操作 #非成员函数
开发语言·c++·stl·string·范围for·auto·string 的使用