This is a follow-on from a previous question (here), but I'm working on a multithreaded application and I would like to post a Boost packaged_task to a threaded io_service. I'm stuck using a C++03 compiler (so std::move is out), and the packaged_task is not copyable. I've tried wrapping it in a shared_ptr and passing that, and a lot of other things. Here is my current attempt and the subsequent compiler errors. Any idea how to get this to work?
boost::asio::io_service io_service;
boost::thread_group threads;
boost::asio::io_service::work work(io_service);
for (int i = 0; i < maxNumThreads; ++i)
{
threads.create_thread(boost::bind(&boost::asio::io_service::run,
&io_service));
}
std::vector<boost::shared_future<bool> > pending_data; // vector of futures
bool process_data(int,int){...}
...
for(int theTime = 0; theTime != totalScenarioTime; ++theTime)
{
for(int i = 0; i < numSmallTasks; ++i)
{
boost::packaged_task<bool> task(boost::bind(&process_data,i,theTime));
boost::shared_future<bool> fut(task.get_future());
pending_data.push_back(fut); // C++11 possible: (std::move(fut) when fut is a unique_future);
io_service.post(task); // C++11 possible: (std::move(task));
}
// After loop - wait until all futures are evaluated
boost::wait_for_all(pending_data.begin(), pending_data.end());
pending_data.clear();
}
This results in:
In file included from ../boostlibs/boost/asio/io_service.hpp:767:0,
from ../boostlibs/boost/asio/basic_io_object.hpp:19,
from ../boostlibs/boost/asio/basic_socket.hpp:19,
from ../boostlibs/boost/asio/basic_datagram_socket.hpp:20,
from ../boostlibs/boost/asio.hpp:20,
from ../main.cpp:13:
../boostlibs/boost/asio/impl/io_service.hpp: In member function ‘void boost::asio::io_service::post(const CompletionHandler&) [with CompletionHandler = boost::packaged_task<bool>]’:
../main.cpp:256:23: instantiated from here
../boostlibs/boost/asio/impl/io_service.hpp:95:67: error: no matching function for call to ‘boost::packaged_task<bool>::packaged_task(const boost::packaged_task<bool>&)’
../boostlibs/boost/asio/impl/io_service.hpp:95:67: note: candidates are:
../boostlibs/boost/thread/future.hpp:1372:9: note: boost::packaged_task<R>::packaged_task(boost::detail::thread_move_t<boost::packaged_task<R> >) [with R = bool]
../boostlibs/boost/thread/future.hpp:1372:9: note: no known conversion for argument 1 from ‘const boost::packaged_task<bool>’ to ‘boost::detail::thread_move_t<boost::packaged_task<bool> >’
../boostlibs/boost/thread/future.hpp:1318:9: note: boost::packaged_task<R>::packaged_task() [with R = bool]
../boostlibs/boost/thread/future.hpp:1318:9: note: candidate expects 0 arguments, 1 provided
../boostlibs/boost/thread/future.hpp:1314:9: note: boost::packaged_task<R>::packaged_task(boost::packaged_task<R>&) [with R = bool, boost::packaged_task<R> = boost::packaged_task<bool>]
../boostlibs/boost/thread/future.hpp:1314:9: note: no known conversion for argument 1 from ‘const boost::packaged_task<bool>’ to ‘boost::packaged_task<bool>&’
../boostlibs/boost/asio/detail/handler_type_requirements.hpp:95:26: error: initializing argument 1 of ‘T& boost::asio::detail::lvref(T) [with T = boost::packaged_task<bool>]’
../boostlibs/boost/asio/impl/io_service.hpp:95:67: error: no matching function for call to ‘boost::packaged_task<bool>::packaged_task(const boost::packaged_task<bool>&)’
../boostlibs/boost/asio/impl/io_service.hpp:95:67: note: candidates are:
../boostlibs/boost/thread/future.hpp:1372:9: note: boost::packaged_task<R>::packaged_task(boost::detail::thread_move_t<boost::packaged_task<R> >) [with R = bool]
../boostlibs/boost/thread/future.hpp:1372:9: note: no known conversion for argument 1 from ‘const boost::packaged_task<bool>’ to ‘boost::detail::thread_move_t<boost::packaged_task<bool> >’
../boostlibs/boost/thread/future.hpp:1318:9: note: boost::packaged_task<R>::packaged_task() [with R = bool]
../boostlibs/boost/thread/future.hpp:1318:9: note: candidate expects 0 arguments, 1 provided
../boostlibs/boost/thread/future.hpp:1314:9: note: boost::packaged_task<R>::packaged_task(boost::packaged_task<R>&) [with R = bool, boost::packaged_task<R> = boost::packaged_task<bool>]
../boostlibs/boost/thread/future.hpp:1314:9: note: no known conversion for argument 1 from ‘const boost::packaged_task<bool>’ to ‘boost::packaged_task<bool>&’
../boostlibs/boost/asio/detail/handler_type_requirements.hpp:96:32: error: initializing argument 1 of ‘const T& boost::asio::detail::clvref(T) [with T = boost::packaged_task<bool>]’
../boostlibs/boost/asio/impl/io_service.hpp:97:3: error: no matching function for call to ‘boost::packaged_task<bool>::packaged_task(const boost::packaged_task<bool>&)’
../boostlibs/boost/asio/impl/io_service.hpp:97:3: note: candidates are:
../boostlibs/boost/thread/future.hpp:1372:9: note: boost::packaged_task<R>::packaged_task(boost::detail::thread_move_t<boost::packaged_task<R> >) [with R = bool]
../boostlibs/boost/thread/future.hpp:1372:9: note: no known conversion for argument 1 from ‘const boost::packaged_task<bool>’ to ‘boost::detail::thread_move_t<boost::packaged_task<bool> >’
../boostlibs/boost/thread/future.hpp:1318:9: note: boost::packaged_task<R>::packaged_task() [with R = bool]
../boostlibs/boost/thread/future.hpp:1318:9: note: candidate expects 0 arguments, 1 provided
../boostlibs/boost/thread/future.hpp:1314:9: note: boost::packaged_task<R>::packaged_task(boost::packaged_task<R>&) [with R = bool, boost::packaged_task<R> = boost::packaged_task<bool>]
../boostlibs/boost/thread/future.hpp:1314:9: note: no known conversion for argument 1 from ‘const boost::packaged_task<bool>’ to ‘boost::packaged_task<bool>&’
../boostlibs/boost/asio/detail/impl/task_io_service.hpp:54:6: error: initializing argument 1 of ‘void boost::asio::detail::task_io_service::post(Handler) [with Handler = boost::packaged_task<bool>]’
Using boost::move(task) results in the two errors:
error: no match for call to ‘(boost::detail::thread_move_t<boost::packaged_task<bool> >) ()’
error: no match for call to ‘(boost::detail::thread_move_t<boost::packaged_task<bool> >) ()’
boost::packaged_task
supports boost::move
since Boost version 1.50, see corresponding ticket.
But the problem is that io_service::post
completion handler parameter must be CopyConstructible
as noted in Asio handler requirements. Therefore boost::packaged_task
cannot be posted directly or by moving. (Thanks to Igor R. for this issue).
There is workaround using pointers, e.g. you could wrap boost::packaged_task
with boost::shared_ptr
and bind it to operator()
:
typedef boost::packaged_task<bool> task_t;
boost::shared_ptr<task_t> task = boost::make_shared<task_t>(
boost::bind(&process_data, i, theTime));
io_service.post(boost::bind(&task_t::operator(), task));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With