栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ 多线程并发 基础入门教程 1.1 创建线程(thread)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ 多线程并发 基础入门教程 1.1 创建线程(thread)

该博客通过观看b站视频 [C++ 多线程并发 基础入门教程] 1.1 创建线程(thread) 进行的笔记总结,up主讲的非常好,适合了解一点多线程知识,想快速入门的同学

使用总结:

使用解释
std::thread thread1(func, a)创建一个线程并运行
thread1.join()等待子线程thread1
thread1.detach()当前线程与子线程分离
thread1.get_id()获取thread1线程的id
thread1.hardware_concurrency()获取硬件支持的线程数量
std::this_thread::sleep_for()使线程等待
#include 
#include 

void func(int a)
{
    while (true)
    {
        std::cout << "child thread a = "<
    int a = 10;
    // 创建一个线程并运行
    std::thread thread1(func, a);

    // 打印thread1线程的id
    std::cout << "thread id =" << thread1.get_id() << std::endl;

    std::cout << "support num =" << thread1.hardware_concurrency() << std::endl;
    // 等待子线程thread1(阻塞该线程)
    thread1.join();

    // 分离子线程thread1(不推荐使用)
    //thread1.detach();
}


我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号