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;
}
相关推荐
rjszcb30 分钟前
一文说完c++全部基础知识,IO流(二)
c++
小字节,大梦想1 小时前
【C++】二叉搜索树
数据结构·c++
吾名招财1 小时前
yolov5-7.0模型DNN加载函数及参数详解(重要)
c++·人工智能·yolo·dnn
我是哈哈hh2 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝
憧憬成为原神糕手2 小时前
c++_ 多态
开发语言·c++
郭二哈2 小时前
C++——模板进阶、继承
java·服务器·c++
挥剑决浮云 -2 小时前
Linux 之 安装软件、GCC编译器、Linux 操作系统基础
linux·服务器·c语言·c++·经验分享·笔记
丶Darling.2 小时前
LeetCode Hot100 | Day1 | 二叉树:二叉树的直径
数据结构·c++·学习·算法·leetcode·二叉树
labuladuo5202 小时前
Codeforces Round 977 (Div. 2) C2 Adjust The Presentation (Hard Version)(思维,set)
数据结构·c++·算法
小柯J桑_3 小时前
C++:STL简介
c++·stl