How do you post a boost packaged_task to an io_service in C++03?

前端 未结 1 1384
悲哀的现实
悲哀的现实 2021-01-04 09:01

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.

1条回答
  •  借酒劲吻你
    2021-01-04 09:41

    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 task_t;
     boost::shared_ptr task = boost::make_shared(
        boost::bind(&process_data, i, theTime));
    
     io_service.post(boost::bind(&task_t::operator(), task));
    

    0 讨论(0)
提交回复
热议问题