c++ - 重新分配给尚未准备好的 future 时会发生什么

标签 c++ c++11 c++14 future

在代码审查期间,我遇到了一段代码,基本上可以归结为:

#include <iostream>
#include <future>
#include <thread>

int main( int, char ** )
{
    std::atomic<int> x( 0 );
    std::future<void> task;
    for( std::size_t i = 0u; i < 5u; ++i )
    {
        task = std::async( std::launch::async, [&x, i](){
                std::this_thread::sleep_for( std::chrono::seconds( 2u * ( 5u - i ) ) );
                ++x;
            } );
    }

    task.get();
    std::cout << x << std::endl;
    return 0;
}

我不太确定

  • 保证打印出结果时所有任务都执行完毕,
  • 任务是否会一个接一个地执行(即任务分配是否会阻塞)。

我无法通过阅读互联网上的文档来回答这个问题,所以我想我会编写上面的代码片段来了解我们的编译器实际上做了什么。

现在,我发现 gcc-5 所做的事情的答案是优柔寡断的,这让我更加好奇:人们会假设分配是阻塞的或非阻塞的。

如果是阻塞的,那么程序所用的时间基本上应该是各个任务执行所用时间的总和。第一个需要 10 秒,第二个 8 秒,第三个 6 秒,第四个 4 秒,最后 2 秒。所以总共需要 10+8+6+4+2 = 30 秒

如果是非阻塞的,应该和上一个任务一样长,即2秒

发生的情况如下:需要 18 秒(使用时间 ./a.out 或旧时钟测量)。通过对代码进行一些操作,我发现代码的行为就像赋值是交替阻塞和非阻塞一样。

但这不可能是真的,对吧? std::async 可能有一半时间回落到 std::deferred ?我的调试器说它会产生两个线程,阻塞直到两个线程都退出,然后再产生两个线程,依此类推。

标准是怎么说的?应该发生什么? gcc-5 内部发生了什么?

最佳答案

一般来说,task 通过 operator=(&&) 的分配不必是阻塞的(见下文),但由于您创建了 std::future 使用 std::async,这些分配变成阻塞(感谢@T.C.):

[future.async]

If the implementation chooses the launch::async policy,

  • [...]

  • the associated thread completion synchronizes with ([intro.multithread]) the return from the first function that successfully detects the ready status of the shared state or with the return from the last function that releases the shared state, whichever happens first.

为什么执行时间是 18 秒?

在您的情况下发生的情况是 std::async 为您的 lambda 分配启动“线程”-有关如何获得的详细说明,请参见下文执行时间为 18 秒。

这就是(可能)在您的代码中发生的情况(e 代表 epsilon):

  • t = 0,第一个std::async调用i = 0,开始一个新线程;
  • t = 0 + e,第二个 std::async 调用,i = 1 开始一个新线程,然后移动。此举将释放 task 当前的共享状态,阻塞约 10 秒(但第二个 std::asynci = 1 已经在执行);
  • t = 10,第三个 std::async 调用,i = 2 开始一个新线程,然后移动。 task 的当前共享状态是调用 i = 1,它已经准备好,所以没有阻塞;
  • t = 10 + e,第四次 std::async 调用,i = 3 开始一个新线程,然后移动。移动是阻塞的,因为之前 i = 2std::async 尚未准备好,但 i = 3 的线程已经启动;
  • t = 16,第五次 std::async 调用,i = 4 开始一个新线程,然后移动。 task (i = 3)的当前共享状态已经就绪,所以非阻塞;
  • t = 16 + e,跳出循环,调用.get()等待*shared state`准备好;
  • t = 18shared state 准备就绪,整个事情就结束了。

关于std::future::operator=的标准细节:

这是 std::futureoperator= 的标准报价:

future& operator=(future&& rhs) noexcept;

Effects:

  • (10.1) — releases any shared state (30.6.4).
  • ...

这是“释放任何共享状态”的意思(重点是我的):

When an asynchronous return object or an asynchronous provider is said to release its shared state, it means:

(5.1) — [...]

(5.2) — [...]

(5.3) — these actions will not block for the shared state to become ready, except that it may block if all of the following are true: the shared state was created by a call to std::async, the shared state is not yet ready, and this was the last reference to the shared state.

您的情况属于我所强调的(我认为)。您使用 std::async 创建了共享状态,它正在休眠(所以还没有准备好)并且您只有一个对它的引用,所以这可能被阻塞了。

关于c++ - 重新分配给尚未准备好的 future 时会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38141229/

相关文章:

c++ - 在 Windows 10 上将 Armadillo 用作带有 CLion 和 CMake 的库的问题

c++ - 如何检测当前输入的语言?

c++ - 具有未触及的非 constexpr 参数 : Who is correct, clang 或 gcc 的 constexpr?

c++ - 如何让 `std::vector<int32_t>` 从 `std::vector<uint32_t>&&` 获取内存?

c++ - move 构造函数相对于采用 bool 表示是复制还是 move 的复制构造函数有什么优势?

c++ - [dcl.constexpr]/3 中的 contain 是什么意思

c++ - QMainWindow 不使用 setMouseTracking() 跟踪鼠标

c++ - 我是否可以将嵌套的 lua 表作为 C 函数的参数读取?

c++ - 使用带有 std::void_t 的类模板检查默认构造函数

c++ - 背包算法,如何获得更好的性能?