12

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> >) ()’
Community
  • 1
  • 1
John Doe
  • 301
  • 3
  • 11
  • Which Boost version do you use? – Rost Oct 31 '12 at 12:28
  • 1
    In 1.50 boost::move(task) should work. – Igor R. Oct 31 '12 at 12:34
  • @Rost, @IgorR., I was using 1.49, now am using 1.51 and I still get the following error when using boost::move(task): `../boost_1_51_0/boost/asio/handler_invoke_hook.hpp:64:3: error: no match for call to ‘(boost::detail::thread_move_t >) ()’ ` Any ideas? – John Doe Oct 31 '12 at 15:05
  • @JohnDoe Strange thing, what compiler/version do you use? – Rost Oct 31 '12 at 16:55
  • 3
    Oops, sorry, didn't notice you attempt to `post` it. `io_service::post` copies its arguments, so `move` wouldn't help. The only way is to allocate the task dynamically and pass its (shared)pointer - just like @Rost recommended. – Igor R. Oct 31 '12 at 17:40
  • @Rost gcc, 4.6.3, with and without the -std=c++03 flag, the error still occurs. – John Doe Oct 31 '12 at 20:54
  • @JohnDoe Igor is right, I was disoriented by ability of `io_service::post` to take rvalues. But actually handler must be `CopyConstructible`, so `boost::packaged_task` cannot be posted directly or by moving :-( Sorry for inconvience. – Rost Oct 31 '12 at 23:10

1 Answers1

15

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));
Community
  • 1
  • 1
Rost
  • 8,779
  • 28
  • 50
  • your work-around of binding operator() to the shared pointer worked. Thanks so much for your help with this! – John Doe Oct 31 '12 at 15:12
  • I would be very interested to know how this fairs for people using VC++10. While shared packaged-tasks seems to work fine on posix (mac clang and linux gcc), I get very strange errors on windows --> 'member function already defined or declared' – Ben J Sep 26 '13 at 13:01
  • @BenJ I've just compiled this code on VC2010 using Boost 1.53 and it compiles and works OK (except for C4913 warning in future.hpp but this is known Boost issue). Could you please post your actual code? – Rost Oct 07 '13 at 14:10
  • Thanks Rost, yes, this was my mistake. I'm unfortunately having to use boost 1_49 which I've discovered is very buggy when it comes to packaged_tasks and shared_futures and even more so if c++11 compilation is enabled. These issues seem to have been fixed in later versions of boost – Ben J Oct 08 '13 at 09:54