• 感谢大家访问本站,希望本站的内容可以帮助到大家!

  • 工资「喂饱肚子」,副业「养活灵魂」!

  • 如果觉得本站的内容有帮助,可以考虑打赏博主哦!

  • 本站由于前段时间遭受到大量临时和国外邮箱注册,所以对可注册的邮箱类型进行了限制!

  • 计算机图形学与计算几何经典必备书单整理,下载链接可参考:https://www.stubbornhuang.com/1256/

  • 欢迎大家交换友链,可在https://www.stubbornhuang.com/申请友情链接进行友链交换申请!

  • 问题反馈可发送邮件到stubbornhuang@qq.com

  • 在本站开通年度VIP,无限制下载本站资源和阅读本站文章

  • 本站会放置Google广告用于维持域名以及网站服务器费用。

C++11/std::thread – 线程的基本用法

C++ 发布于2020-04-01 阅读 6,151次 0次评论 0次点赞 本文共1621个字,阅读需要5分钟。

1 获取CPU核心数量

使用std::thread::hardware_concurrency()获取当前CPU核心数量。
代码示例:

#include <iostream>
#include <thread>


int main()
{
    std::cout << std::thread::hardware_concurrency() << std::endl;
    getchar();
    return 0;
}

2 获取当前线程ID

get_id()可用于获取当前线程的id。
代码示例:

#include <iostream>
#include <thread>


int main()
{
    std::thread thread{ [] {std::cout << "线程函数被启动" << std::endl; } };
    std::cout << thread.get_id() << std::endl;

    thread.join();

    getchar();
    return 0;
}

3 线程休眠

3.1 sleep_for让当前线程休眠一段时间

#include <iostream>
#include <thread>


int main()
{
    std::thread thread{ [] {std::cout << "线程函数被启动" << std::endl; } };
    std::this_thread::sleep_for(std::chrono::seconds(1));

    std::cout << thread.get_id() << std::endl;

    thread.join();

    getchar();
    return 0;
}

3.2 sleep_until让当前线程休眠一直到指定时间

#include <iostream>       
#include <iomanip>        
#include <thread>         
#include <chrono>         
#include <ctime>          

#pragma warning(disable:4996)//加上可去掉unsafe 请使用localtime_s的编译报错

int main()
{
    std::thread thread{ [] {std::cout << "线程函数被启动" << std::endl; } };

    std::time_t tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
    struct std::tm * ptm = std::localtime(&tt);

    std::cout << "当前时间为: " << std::put_time(ptm, "%X") << std::endl;    

    // 设定线程被唤醒时间为1分钟后
    ptm->tm_min += 1;

    std::this_thread::sleep_until(std::chrono::system_clock::from_time_t(mktime(ptm)));

    std::cout << thread.get_id() << std::endl;

    std::cout << "线程重新被唤起时间为:" << std::put_time(ptm, "%X") << std::endl;;

    thread.join();

    getchar();
    return 0;
}

4 yield

yield放弃当前线程执行,回到准备状态,重新分配cpu资源。所以调用该方法后,可能执行其他线程,也可能还是执行该线程。

#include <iostream>             
#include <thread>         
#include <chrono>          


int main()
{
    bool ready = false;
    std::thread thread{ [&] {
        while (!ready)
        {
            std::cout << "线程函数被挂起" << std::endl;
            std::this_thread::yield();
            ready = true;
        }    
        std::cout << "线程函数被启动" << std::endl;
    } };


    thread.join();

    getchar();
    return 0;
}

欢迎扫码关注我的微信公众号,及时获取文章更新

微信公众号二维码

本文作者:StubbornHuang

版权声明:本文为站长原创文章,如果转载请注明原文链接!

原文标题:C++11/std::thread – 线程的基本用法

原文链接:https://www.stubbornhuang.com/777/

发布于:2020年04月01日 11:57:12

修改于:2023年06月26日 22:29:54

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

文章末尾
上一篇
C++11/std::thread - 线程管理join/detach
C++
下一篇
C++11/std::condition_variable - 生产者消费者模型
C++
当前分类随机文章推荐

发表评论

您必须 [ 登录 ] 才能发表留言!

关注我们的公众号

微信公众号