C++多线程运行整理

一.多线程运行

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

void print_id(int id)
{
    std::cout << "ID:" << id <<  ",线程ID为:" << std::this_thread::get_id() <<std::endl;
}
int main(void)
{   
    std::thread t1(print_id,1);
    t1.join();
    std::thread t2(print_id,2);
    t2.join();
    return 0;
}

二.多线程并发的实现方式

1.使用函数指针

复制代码
// 多线程并发1_使用函数指针
// 使用多线程的库,来实现并行计算的加法

#include <iostream>   
#include <thread>

int sum = 0;
float avg = 0;
void add(int a,int b)
{
    sum += a + b;
}

void Avg(int a,int b)
{
    avg = (a + b)/2;
}

int main()
{
    int a = 5;
    int b = 10;
    std::thread t1(add,a,b);
    t1.join();
    std::cout << "线程1和为:" << sum <<std::endl;
    
    std::thread t2(add,a,b);
    t2.join();
    std::cout << "线程2和为:" << sum <<std::endl;
    
    std::thread t3(Avg,a,b);
    t3.join();
    std::cout << "线程3平均为:" << avg <<std::endl;
    return 0;
}

|---------------------|-----------------|---------------------------------|
| 全局变量无锁保护 | 多线程共享变量,有数据竞争风险 | 使用 std::mutex |
| std::thread 无法返回值 | 不能直接获取线程函数的返回值 | 改用 std::async + std::future |

2.使用函数对象

复制代码
// 多线程并发2_使用函数对象
// 通过类中的 operator() 方法定义函数对象来创建线程

#include <iostream>   
#include <thread>

class PrintTask
{
public:
    void operator()(int count) const{
        for (int i = 0;i < count;++i){
            std::cout << "第" << i+1 << "次使用函数对象(function object)创建多线程!\n";
        }
    }
};

int main (){
    std::thread t2(PrintTask(),5);//创建线程,传递函数对象和参数
    t2.join(); //等待线程完成
    return 0;
}

3.使用Lambda表达式

复制代码
// 多线程并发3_使用Lambda表达式
// 使用Lambda表达式可以直接内联定义线程执行的代码

#include <iostream>   
#include <thread>
int main(){
    std::thread t3([](int count){
        for (int i = 0;i < count; ++i){
            std::cout << "第" << i+1 << "次使用Lambda表达式执行线程!\n" ;
        }
    },5);//创建线程,传递Lambda表达式和参数
    t3.join();//等待线程完成
    return 0;
}

三.整合3种多线程并发创建方式融合展示

复制代码
/*
将函数指针,函数对象和Lambda表达式 3种方式创建线程进行融合展示
*/

#include <iostream>
#include <thread>
using namespace std;

//一个简单的函数,作为线程的入口函数
void foo(int Z){
    for (int i = 0;i < Z;i++){
        cout << "线程使用函数指针作为可调用参数\n";
    }
}

//可调用对象的类定义
class ThreadObj{
    public:
        void operator()(int x) const{
            for (int i = 0;i < x;i++){
                cout << "线程使用函数对象作为可调用参数\n";
            }
        }
};

int main(){
    cout << "线程1,2,3独立运行" << endl;
    //使用函数指针创建线程
    thread th1(foo,3);

    //使用函数对象创建线程
    thread th2(ThreadObj(),3);

    //使用Lambda表达式创建线程
    thread th3([] (int x){
        for (int i = 0;i < x;i++){
            cout << "线程使用Lambda表达式作为可调用参数\n";
        }
    },3);

    //等待所有线程完成
    th1.join(); //等待线程th1完成
    th2.join(); //等待线程th2完成
    th3.join(); //等待线程th3完成
    return 0;
}

整理不易,诚望各位看官点赞 收藏 评论 予以支持,这将成为我持续更新的动力源泉。若您在阅览时存有异议或建议,敬请留言指正批评,让我们携手共同学习,共同进取,吾辈自当相互勉励!

相关推荐
辰小忆11 分钟前
11 · RunCurveChart —— 实时运行曲线
c++·ui·duilib
利威尔xu31 分钟前
Go Mutex 饥饿模式:正常模式等待者饿死,1ms 阈值自动切换崩溃
开发语言·后端·面试·golang·并发编程
乌暮2 小时前
少写重复 CRUD:用大白话搞懂 MyBatis-Plus
java·开发语言·spring boot·面试·mybatis
不绝1912 小时前
Lua基础语法入门:注释、变量类型与字符串操作
开发语言·lua
Hhy_11073 小时前
《C++深度解构07》手撕 vector —— 从底层三指针架构到迭代器失效彻底吃透
c语言·开发语言·c++·学习·visual studio
风落青芜3 小时前
链式二叉树的实现与基本操作
c语言·开发语言·数据结构·算法
艾莉丝努力练剑3 小时前
【QT】系统相关:TCP的API介绍
开发语言·前端·c++·人工智能·qt·学习·tcp/ip
青少儿编程课堂4 小时前
蓝桥杯·全国青少年信息素养大赛·电子学会Python/C++考级:中国剩余定理解析
c++·python·算法·bfs·信息学竞赛
wo_jack4 小时前
C++ 入门笔记(十八):list 进阶 —— const 迭代器、operator-> 与 clear 的坑
数据结构·c++·面试·stl·list
wuxianda10305 小时前
Swift 项目用 Xcode 脚手架起步:默认工程结构和依赖组合为什么会被识别成同源
开发语言·xcode·swift·4.3a·appstore审核