简体   繁体   English

C ++ 11 future :: wait_for编译错误

[英]C++11 future::wait_for compile error

i am trying to make multi-threading application where every thread will be proccessing task for different time. 我正在尝试制作多线程应用程序,其中每个线程将在不同的时间处理任务。 So i want to use future and future::wait_for function. 所以我想使用future和future :: wait_for函数。 But when i use only code from CPP reference 但是当我仅使用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';
}

I get compile error: 我得到编译错误:

 thread.cpp:31:58: error: cannot convert ‘bool’ to ‘std::future_status’ in assignment

I am using ubuntu 12.04 and gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) Any thoughts ? 我正在使用ubuntu 12.04和gcc版本4.6.3(Ubuntu / Linaro 4.6.3-1ubuntu5)有什么想法吗? Thanks ! 谢谢 !

G++ 4.6.3 is not supporting C++11 fully, so updating to a later version that has full support for C++11 (which is gcc 4.7 or later) will solve this sort of issue. G ++ 4.6.3不完全支持C ++ 11,因此更新到对C ++ 11完全支持的更高版本(gcc 4.7或更高版本)将解决此类问题。 Or use clang 3.4 (3.2 supports a lot of C++11, but 3.4 supports a lot more and has better optimisation on top of that). 或使用clang 3.4(3.2支持很多C ++ 11,但是3.4支持很多,并且在此基础上具有更好的优化)。

According to this commit log, the return type was changed from bool to std::future_status in Feb 2012 and GCC 4.7 (on Mar 22, 2012) was the first release shipped with the new version of wait_for . 根据此提交日志,返回类型在2012年2月从bool更改为std::future_status ,GCC 4.7(在2012年3月22日发布)是新版本的wait_for附带的第一个发行版。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM