Pergunta

I'm following some tutorials by Bartosz Milewski here which I find very useful. However, as the author uses the just::thread implementation of the C++11 threading standard (which I don't yet have), I have decided to go with boost threads for now as the author of the tutorial says its trivial to do so. This seems to be the case for the first three tutorials in the series but I bumped into some problems with the fourth. Here is my code:

#include <iostream>
#include <cassert>
#include <boost\thread.hpp>
#include <boost\thread\future.hpp>

void thFun(boost::promise<std::string> & prms)
{
    std::string str("Hi from future!");
    prms.set_value(str);
}

int main()
{
    boost::promise<std::string> prms;
    boost::unique_future<std::string> fut = prms.get_future();

    boost::thread th(&thFun, std::move(prms)); // error C2248: 'boost::promise<R>::promise' : cannot access private member declared in class 'boost::promise<R>' 

    std::cout << "Hi from main!";
    std::string str = fut.get();
    std::cout << str << std::endl;
    th.join();

    return 0;
}

The following line seems to raise an issue that I don't understand:

boost::thread th(&thFun, std::move(prms));

where the compiler complains:

error C2248: 'boost::promise::promise' : cannot access private member declared in class 'boost::promise'

Can anyone suggest what the problem might be?

thanks in advance!

Foi útil?

Solução

boost::thread uses boost::bind to handle a thread function with additional arguments, which requires that they are copyable. You could pass the promise by pointer or reference (using e.g. boost::ref), but that requires that the object outlives the thread. In this example it is OK, but for a detached thread, or one which outlives the function that started it, this would prevent the use of boost::promise objects on the stack.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top