Setting limit on post queue size with Boost Asio?

后端 未结 4 1571
慢半拍i
慢半拍i 2021-02-20 18:09

I\'m using boost::asio::io_service as a basic thread pool. Some threads get added to io_service, the main thread starts posting handlers, the worker threads start r

4条回答
  •  -上瘾入骨i
    2021-02-20 18:46

    I'm using the semaphore to fix the handlers queue size. The following code illustrate this solution:

    void Schedule(boost::function function)
    {
        semaphore.wait();
        io_service.post(boost::bind(&TaskWrapper, function));
    }
    
    void TaskWrapper(boost::function &function)
    {
        function();
        semaphore.post();
    }
    

提交回复
热议问题