Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么~future在由std::async创建时阻塞,而不是由其他人创建时阻塞_C++_Multithreading_Asynchronous_C++14_Libc++ - Fatal编程技术网

C++ 为什么~future在由std::async创建时阻塞,而不是由其他人创建时阻塞

C++ 为什么~future在由std::async创建时阻塞,而不是由其他人创建时阻塞,c++,multithreading,asynchronous,c++14,libc++,C++,Multithreading,Asynchronous,C++14,Libc++,std::async返回: 类型为futureWhat实现的对象?至少有3种主要的std实现,而标准没有指定此功能的工作方式。我使用GCC10.2及其附带的标准库std::future非常糟糕。真正地对于也支持co_wait、执行者等的“期货”,您可以使用我的“请查看”问题:哪个实现?至少有3种主要的std实现,而标准没有指定此功能的工作方式。我使用GCC10.2及其附带的标准库std::future非常糟糕。真正地对于同样支持co_wait、执行人等的“期货”,您可以使用我的“请查看此问题”:

std::async
返回:


类型为
futureWhat实现的对象?至少有3种主要的std实现,而标准没有指定此功能的工作方式。我使用GCC10.2及其附带的标准库
std::future
非常糟糕。真正地对于也支持
co_wait
、执行者等的“期货”,您可以使用我的“请查看”问题:哪个实现?至少有3种主要的std实现,而标准没有指定此功能的工作方式。我使用GCC10.2及其附带的标准库
std::future
非常糟糕。真正地对于同样支持
co_wait
、执行人等的“期货”,您可以使用我的“请查看此问题”:
#include <chrono>
#include <future>
#include <iostream>
#include <thread>

int main()
{
    using namespace std::literals;
    // get a std::future<> by std::async<>();
    std::future<int> BlockingFuture = std::async([]() {std::this_thread::sleep_for(1s); return 1; });

    // get a std::future<> by packaged_task<>
    std::packaged_task<int()> task([]() {std::this_thread::sleep_for(1s); return 2; });
    std::future<int>          NonBlockingFuture = task.get_future();
    std::thread               task_thread(std::move(task));

    std::cout << "BlockingFuture.get(): " << BlockingFuture.get() << std::endl
              << "NonBlockingFuture.get(): " << NonBlockingFuture.get() << std::endl;

    task_thread.join();
    return 0;
}