Question

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. But when i use only code from CPP reference

#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 ? Thanks !

Was it helpful?

Solution

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. 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).

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top