[C++]c++11中std::async 和std::thread的区别

td::async和std::thread都是C++11中提供的线程库,它们都可以用于创建新线程,本文主要介绍了c++11之std::async 和std::thread的区别小结,感兴趣的可以了解一下

std::async和std::thread都是C++11中提供的线程库,它们都可以用于创建新线程。它们的主要区别在于:

  • std::async有时候并不创建新线程,而是使用线程池中的线程来执行任务,这取决于实现。而std::thread总是创建新线程。
  • std::async返回一个std::future对象,可以用来获取异步任务的结果。而std::thread没有返回值,需要通过其他方式来获取线程执行的结果。
  • std::async可以指定任务的执行策略,例如std::launch::async表示一定要创建新线程执行任务,std::launch::deferred表示可以不创建新线程,等到调用std::future的get()方法时再执行任务。而std::thread没有这样的选项。
#include <iostream>
#include <future>
#include <thread>

int task()
{
    std::cout << "Task is running in thread " << std::this_thread::get_id() << std::endl;
    return 42;
}

int main()
{
    // 使用std::async创建异步任务
    std::future<int> f1 = std::async(std::launch::async, task);
    std::cout << "Async task has been started." << std::endl;

    // 使用std::thread创建新线程
    std::thread t(task);
    std::cout << "Thread has been started." << std::endl;

    // 等待异步任务完成并获取结果
    int result1 = f1.get();
    std::cout << "Async task has been finished with result " << result1 << std::endl;

    // 等待线程完成并退出
    t.join();
    std::cout << "Thread has been finished." << std::endl;

    return 0;
}

更直观的看一下std::launch::async 与 ,std::launch::deferred

#include <iostream>
#include <future>

int main() {
    auto f1 = std::async(std::launch::deferred, []() {
        std::cout << "This is a deferred async task." << std::endl;
        return 1;
        });
    auto f2 = std::async(std::launch::async, []() {
        std::cout << "This is a async task." << std::endl;
        return 2;
        });
    std::cout << "Waiting..." << std::endl;
    std::cout << f1.get() << std::endl;
    std::cout << f2.get() << std::endl;
    return 0;
}

std::async不确定问题的解决

不加额外参数的std::async调用问题,让系统自行决定是否创建新的线程。

问题的焦点在于 std::future<int> result = std::async(mythread)写法,这个异步任务到底 有没有被推迟执行。

实例代码如下:

#include<iostream>
#include<thread>
#include<string>
#include<vector>
#include<list>
#include<mutex>
#include<future>
using namespace std;
int mythread() //线程入口函数
{
    cout << "mythread start" << "threadid= " << std::this_thread::get_id() << endl; //打印线程id
    std::chrono::milliseconds dura(5000); //定一个5秒的时间
    std::this_thread::sleep_for(dura);  //休息一定时常
    cout << "mythread end" << "threadid= " << std::this_thread::get_id() << endl; //打印线程id
    return 5;
}
int main()
{
    cout << "main" << "threadid= " << std::this_thread::get_id() << endl;
    std::future<int> result = std::async(mythread);//流程并不卡在这里
    cout << "continue....." << endl;
    //枚举类型
    std::future_status status = result.wait_for(std::chrono::seconds(0));//等待一秒
    
    if (status == std::future_status::deferred)
    {
        //线程被延迟执行了,系统资源紧张
        cout << result.get() << endl; //此时采取调用mythread()
    }
    else if (status == std::future_status::timeout)//
    {
        //超时:表示线程还没执行完;我想等待你1秒,希望你返回,你没有返回,那么 status = timeout
        //线程还没执行完
        cout << "超时:表示线程还没执行完!" << endl;
    }
    else if (status == std::future_status::ready)
    {
        //表示线程成功返回
        cout << "线程成功执行完毕,返回!" << endl;
        cout << result.get() << endl;
    }
    cout << "I love China!" << endl;
    return 0;
}

 

 

 

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FL1768317420

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值