相关疑难解决方法(0)

std::async 如何“存储”任意异常?

我无法理解如何std::async存储任何异常,而不仅仅是从std::exception. 我玩弄下面的代码

#include <iostream>
#include <future>
#include <chrono>

void f()
{
    std::cout << "\t\tIn f() we throw an exception" << std::endl;
    throw 1; // throw an int
}

int main()
{
    std::future<void> fut = std::async(std::launch::async, f);
    std::cout << "Main thread sleeping 1s..." << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(1)); // sleep one second
    std::cout << "Main thread waking up" << std::endl;
    try
    {
        fut.get();
    }
    catch(...)
    {
        std::cout << "We caught an exception!" << std::endl;
        throw; // rethrow
    }
} …
Run Code Online (Sandbox Code Playgroud)

c++ asynchronous future exception c++11

6
推荐指数
1
解决办法
1778
查看次数

标签 统计

asynchronous ×1

c++ ×1

c++11 ×1

exception ×1

future ×1