我正在尝试使用在线程中启动一个函数std::packaged_task
Query query; /* protobuf object */
/* fill Query object */
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:再次更新代码以修复错误并包含完整的错误消息。
g++-4.6(在 Ubuntu 10.04 上)无法编译代码:
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
我读过这可能是由于一个错误:gcc-mailinglist
我是 C++/C++11 的新手 - 什么是一个好的工作选择?我只需要启动一个给我未来的线程,get()
稍后在boost::asio
异步循环中调用 who -method。