I am trying to run a function in a stream using std::packaged_task
Query query;
std::packaged_task<SearchResults(Query&)> task([](Query& q) ->SearchResults {
index::core::Merger merger;
return merger.search(q);
});
std::future<SearchResults> ftr = task.get_future();
std::thread(std::move(task),query).detach();
Edit2: updated the code again to fix the errors and included the full error message.
g ++ - 4.6 (according to Ubuntu 10.04) cannot compile code:
In file included from /usr/include/c++/4.6/memory:80:0,
from ../src/net/QueryConnection.cpp:8:
/usr/include/c++/4.6/functional: In instantiation of ‘std::_Bind_result<void,
std::packaged_task<SearchResults(Query&)>(Query)>’:
/usr/include/c++/4.6/thread:135:9: instantiated from ‘std::thread::thread(_Callable&&,
_Args&& ...) [with _Callable = std::packaged_task<SearchResults(Query&)>, _Args =
{Query&}]’
../src/net/QueryConnection.cpp:77:36: instantiated from here
/usr/include/c++/4.6/functional:1365:7: error: ‘std::_Bind_result<_Result,
_Functor(_Bound_args ...)>::_Bind_result(const std::_Bind_result<_Result,
_Functor(_Bound_args ...)>&) [with _Result = void, _Functor =
std::packaged_task<SearchResults(Query&)>, _Bound_args = {Query},
std::_Bind_result<_Result, _Functor(_Bound_args ...)> = std::_Bind_result<void,
std::packaged_task<SearchResults(Query&)>(Query)>]’ declared to take const reference,
but implicit declaration would take non-const
Build error occurred, build is stopped
I read that this is possible due to an error: gcc-mailinglist
I'm new to C ++ / C ++ 11 - What would be a good working alternative? I just need to start the thread that gives me the future — the get()method is called later in the async loop boost::asio.
source
share