[英]C++11 future::wait_for compile error
我正在嘗試制作多線程應用程序,其中每個線程將在不同的時間處理任務。 所以我想使用future和future :: wait_for函數。 但是當我僅使用CPP參考中的代碼時
#include <iostream>
#include <future>
#include <thread>
#include <chrono>
int main()
{
std::future<int> future = std::async(std::launch::async, [](){
std::this_thread::sleep_for(std::chrono::seconds(3));
return 8;
});
std::cout << "waiting...\n";
std::future_status status;
do {
status = future.wait_for(std::chrono::seconds(1));
if (status == std::future_status::deferred) {
std::cout << "deferred\n";
} else if (status == std::future_status::timeout) {
std::cout << "timeout\n";
} else if (status == std::future_status::ready) {
std::cout << "ready!\n";
}
} while (status != std::future_status::ready);
std::cout << "result is " << future.get() << '\n';
}
我得到編譯錯誤:
thread.cpp:31:58: error: cannot convert ‘bool’ to ‘std::future_status’ in assignment
我正在使用ubuntu 12.04和gcc版本4.6.3(Ubuntu / Linaro 4.6.3-1ubuntu5)有什么想法嗎? 謝謝 !
G ++ 4.6.3不完全支持C ++ 11,因此更新到對C ++ 11完全支持的更高版本(gcc 4.7或更高版本)將解決此類問題。 或使用clang 3.4(3.2支持很多C ++ 11,但是3.4支持很多,並且在此基礎上具有更好的優化)。
根據此提交日志,返回類型在2012年2月從bool
更改為std::future_status
,GCC 4.7(在2012年3月22日發布)是新版本的wait_for
附帶的第一個發行版。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.