C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > c++11 std::async 和std::thread

c++11之std::async 和std::thread的区别小结

作者:奥特神龟1.1

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

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

#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;
}

到此这篇关于c++11之std::async 和std::thread的区别小结的文章就介绍到这了,更多相关c++11 std::async 和std::thread内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文