你如何张贴升压packaged_task在C ++ 03的io_service对象?(How do

2019-07-03 11:55发布

这是一个后续从以前的问题( 在这里 ),但我工作的多线程应用程序,我想一个升压packaged_task张贴到螺纹io_service对象。 我使用插着C ++编译器03(这样的std ::招出),并且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 ::移动(任务)获得了两个错误:

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

Answer 1:

boost::packaged_task支持boost::move ,因为加速1.50版本,请参阅相应的车票 。

但问题是, io_service::post完成处理程序参数必须是CopyConstructible中指出短耳处理程序的要求 。 因此boost::packaged_task不能直接或者通过移动公布。 (感谢伊戈尔R.这个问题)。

有使用指针,例如,你可以换变通boost::packaged_taskboost::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));


文章来源: How do you post a boost packaged_task to an io_service in C++03?