簡體   English   中英

C ++ 11 future.wait_for()總是返回future_status :: timeout

[英]C++11 future.wait_for() always returns future_status::timeout

我有一個C ++ 11程序,檢查一個數字是否為素數。 程序等待准備就緒的未來對象。 准備就緒后,程序會告訴未來對象的提供者功能是否認為該數字是素數。

// future example
#include <iostream>       // std::cout
#include <future>         // std::async, std::future
#include <chrono>         // std::chrono::milliseconds


const int number = 4; // 444444443

// a non-optimized way of checking for prime numbers:
bool is_prime (int x) {
    for (int i=2; i<x; ++i) if (x%i==0) return false;
      return true;
    }

int main ()
{
    // call function asynchronously:
    std::future<bool> fut = std::async (is_prime, number); 

    // do something while waiting for function to set future:
    std::cout << "checking, please wait";
    std::chrono::milliseconds span (100);
    //std::chrono::duration<int> span (1);

    while (fut.wait_for(span)==std::future_status::timeout) {
        std::cout << '.';
        std::cout.flush();
    }

    bool x = fut.get();     // retrieve return value

    std::cout << "\n"<<number<<" " << (x?"is":"is not") << " prime.\n";

    return 0;
}

如果運行該程序,您將看到它處於無限循環中,因為wait_for()總是返回future_status::timeout ,這意味着共享狀態永遠不會准備好。 這是什么原因? 我從http://www.cplusplus.com/reference/future/future/wait_for/上獲取了這個程序,所以我希望它可以工作。 但是,如果我注釋掉while循環,程序將正常工作。

代碼正在運行:(g ++ 4.9,clang 3.4) http://coliru.stacked-crooked.com/a/f3c2530c96591724

我使用MINGW32和g ++ 4.8.1獲得了與你相同的行為。 將策略顯式設置為std::launch::async解決了該問題。
(即: std::async(std::launch::async, is_prime, number);

不確定這是否是編譯器中的錯誤,但我相信wait_for應該返回future_status::deferred這正是Scott Meyers在他的書中的 Effective C++討論的 Item 36:Specify std::launch::async if asynchronicity is essential

他提出的解決方案

修復很簡單:只需檢查與std :: async調用相對應的未來,看看是否延遲了任務,如果是,則避免進入基於超時的循環。 不幸的是,沒有直接的方式來詢問未來其任務是否延期。 相反,您必須調用基於超時的函數 - 諸如wait_for之類的函數。 在這種情況下,你真的不想等待任何事情,你只是想看看返回值是否為std :: future_status :: deferred,所以在必要的情況下扼殺你溫和的懷疑,並在零超時時調用wait_for。

在你的情況下,你可以明確關於異步性,如@Jarod在他的解決方案中提到的,即使用std::launch::async或者你可以重寫你的代碼,如下所示

bool x;
// if task is deferred...
if (fut.wait_for(std::chrono::milliseconds(0)) == std::future_status::deferred)
{
 // ...use wait or get on fut
 // to call is_prime synchronously
   x = fut.get();     // retrieve return value
}
else { // task isn't deferred
 // infinite loop not possible (assuming is_prime finishes)
 while (fut.wait_for(span) != std::future_status::ready) { 
     // task is neither deferred nor ready,
     // so do concurrent work until it's ready
     std::cout << '.';
     std::cout.flush();
 }
 // fut is ready
 x = fut.get();     // retrieve return value
}

http://coliru.stacked-crooked.com/a/cb4e4b3f642f79f5

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM