Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting limit on post queue size with Boost Asio?

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 running the handlers, and everything finishes. So far, so good; I get a nice speedup over single-threaded code.

However, the main thread has millions of things to post. And it just keeps on posting them, much faster than the worker threads can handle them. I don't hit RAM limits, but it's still kind of silly to be enqueuing so many things. What I'd like to do is have a fixed-size for the handler queue, and have post() block if the queue is full.

I don't see any options for this in the Boost ASIO docs. Is this possible?

like image 325
Jon Stewart Avatar asked Jul 26 '12 22:07

Jon Stewart


1 Answers

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

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

void TaskWrapper(boost::function<void()> &function)
{
    function();
    semaphore.post();
}
like image 130
Alexander Panin Avatar answered Sep 20 '22 13:09

Alexander Panin