Namespaces
Variants

std::future<T>:: get

From cppreference.net
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
Safe reclamation
Hazard pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11) (deprecated in C++20)
(C++11) (deprecated in C++20)
Memory ordering
(C++11) (deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
主模板
T get ( ) ;
(1) (C++11 起)
std:: future < T & > 特化
T & get ( ) ;
(2) (C++11 起)
std:: future < void > 特化
void get ( ) ;
(3) (C++11 起)

get 成员函数通过调用 wait() 等待共享状态就绪,随后获取存储在共享状态中的值(如果存在)。调用此函数后, valid() 将变为 false

如果在调用此函数前 valid() false ,则行为未定义。

目录

返回值

1) 存储在共享状态中的值 v ,以 std :: move ( v ) 的形式传递。
2) 作为值存储在共享状态中的引用。
3) (无)

异常

如果在 future 引用的共享状态中存储了异常(例如通过调用 std::promise::set_exception() ),那么该异常将被抛出。

注释

C++标准建议实现应在调用前检测到 valid() false 的情况,并抛出带有错误码 std::future_error 的异常,其错误条件为 std::future_errc::no_state

示例

#include <chrono>
#include <future>
#include <iostream>
#include <string>
#include <thread>
std::string time()
{
    static auto start = std::chrono::steady_clock::now();
    std::chrono::duration<double> d = std::chrono::steady_clock::now() - start;
    return "[" + std::to_string(d.count()) + "s]";
}
int main()
{
    using namespace std::chrono_literals;
    {
        std::cout << time() << " launching thread\n";
        std::future<int> f = std::async(std::launch::async, []
        {
            std::this_thread::sleep_for(1s);
            return 7;
        });
        std::cout << time() << " waiting for the future, f.valid() = "
                  << f.valid() << '\n';
        int n = f.get();
        std::cout << time() << " f.get() returned " << n << ", f.valid() = "
                  << f.valid() << '\n';
    }
    {
        std::cout << time() << " launching thread\n";
        std::future<int> f = std::async(std::launch::async, []
        {
            std::this_thread::sleep_for(1s);
            return true ? throw std::runtime_error("7") : 7;
        });
        std::cout << time() << " waiting for the future, f.valid() = "
                  << f.valid() << '\n';
        try
        {
            int n = f.get();
            std::cout << time() << " f.get() returned " << n
                      << ", f.valid() = " << f.valid() << '\n';
        }
        catch (const std::exception& e)
        {
            std::cout << time() << " caught exception " << e.what()
                      << ", f.valid() = " << f.valid() << '\n';
        }
    }
}

可能的输出:

[0.000004s] launching thread
[0.000461s] waiting for the future, f.valid() = 1
[1.001156s] f.get() returned with 7, f.valid() = 0
[1.001192s] launching thread
[1.001275s] waiting for the future, f.valid() = 1
[2.002356s] caught exception 7, f.valid() = 0

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 应用于 发布时行为 正确行为
LWG 2096 C++11 重载 (1) 需要检查 T 是否为 MoveAssignable 无需检查

参见

检查 future 是否具有共享状态
(公开成员函数)