C++11 future::wait_for compile error
C++11 future::wait_for compile error
我正在尝试制作多线程应用程序,其中每个线程都将在不同的时间内处理任务。所以我想使用future和future::wait_fo函数。但当我只使用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 << "deferredn";
} else if (status == std::future_status::timeout) {
std::cout << "timeoutn";
} 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
新版本附带的第一个版本。
- https://github.com/mirrors/gcc/commit/f7273180cc277a28a6779a21bfa5d5e878254c5f
相关文章:
- "error: no matching function for call to"构造函数错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- C++错误:"error: int aaa::bbb is protected within this context"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 使用 cmake 的 LLVM 构建在 tsan_libdispatch_mac.cc 期间失败; "Error: conflicting types for ..."
- C++ "error: invalid use of void expression"
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 从不同目录执行时"symbol lookup error:",否则从构建目录执行时按预期工作
- Compile Error with Eluna - QueryResult.cpp
- Luarocks luasql-mysql compile error
- 构建 OpenCL 内核时"Compile Server Error."
- BeagleBoneBlack Cross Compile on Mac Error
- C++11 future::wait_for compile error
- c++ std::hash specilization trait compile error
- Boost asio TCP IP asio class compile error with Visual Studio.错误:C2228:'.close'左侧必须具有类/结构/联合
- Cocos2D-X compile Linux Error: Undefined Reference