你如何在C ++ 03中将boost packaged_task发布到io_service?

时间:2012-10-31 12:03:03

标签: multithreading boost boost-asio future

这是前一个问题(here)的后续内容,但我正在研究一个多线程应用程序,我想将一个Boost packaged_task发布到一个线程化的io_service。我坚持使用C ++ 03编译器(所以std :: move已经出局),并且packaged_task不可复制。我已经尝试将它包装在shared_ptr中并传递它以及许多其他内容。这是我当前的尝试和后续的编译器错误。知道如何让这个工作吗?

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();
}

这导致:

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>]’

使用boost :: move(task)会导致两个错误:

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> >) ()’

1 个答案:

答案 0 :(得分:14)

boost::packaged_task自Boost版本1.50起支持boost::move,请参阅相应的ticket

但问题是io_service::post完成处理程序参数必须为CopyConstructible,如Asio handler requirements中所述。因此,boost::packaged_task无法直接发布或移动。 (感谢此问题的Igor R.)。

有使用指针的解决方法,例如你可以用boost::packaged_task包裹boost::shared_ptr并将其绑定到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));