简体   繁体   中英

Enforce maximum size for boost ASIO queue

Many examples I've found online suggest using a queue to manage messages being used by async_write, which in turn use a completion handler i..e, lambda, that discards data once the async_write is complete. For example, the chat server from boost examples: http://www.boost.org/doc/libs/1_62_0/doc/html/boost_asio/example/cpp11/chat/chat_server.cpp . The applicable code is the following:

  void do_write()
  {
    auto self(shared_from_this());
    boost::asio::async_write(socket_,
        boost::asio::buffer(write_msgs_.front().data(),
          write_msgs_.front().length()),
        [this, self](boost::system::error_code ec, std::size_t /*length*/)
        {
          if (!ec)
          {
            write_msgs_.pop_front();
            if (!write_msgs_.empty())
            {
              do_write();
            }
          }
          else
          {
            room_.leave(shared_from_this());
          }
        });
  }

However, in the application I'm using it for, it's possible that messages are pushed into write_msgs_ faster than the async_write can complete and thus the queue grows arbitrarily large in memory until the application crashes.

I tried using a thread safe queue that pops messages off of the front if the queue reaches a certain size. The problem is that the completion handler is the only one that gets to decide when data is finished being used, thus my approach failed since it would pop messages off before they are done being used.

Can anyone provide a push in the right direction? Could I use two queues to solve this problem?

You need a bounded queue, and you need to handle somehow the case of the queue being full.

If you do not need thread safety (ie if you use a strand) you can consider boost::circular_buffer .

Otherwise, there is boost::sync_bounded_queue in Boost.Thread .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM