提高ASIO服务队列深度和政策 [英] boost asio service queue deepness and policies

查看:194
本文介绍了提高ASIO服务队列深度和政策的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解正在运行的ASIO服务就像是一个队列,我可以用它来发布任务的线程将顺序执行。然而,正如任何队列,我想有限制。是否有可能设置这个限制ASIO服务?是否有可能设置遵循什么策略时队列已满(即阻塞,非阻塞,等等)?

I understand a running asio service is like a queue I can use to post tasks a thread will execute sequentially. However, as any queue, I guess there are limits. Is it possible to set this limit for asio services? Is it possible to set what policy to follow when the queue is full (i.e. blocking, non blocking, etc.)?

更新

假设我有运行的ASIO ::服务和定时发布各为10ms到这个线程任务的线程。该任务的接收被绑定到一个方法invokation这将使得线程沉睡100毫秒。我因此计时器张贴100任务的第二到一个线程能够执行10个任务每个第二的。很明显,这种情况会出现分歧。然而,队列打交道时,通常有指标注队列深度(100?1000?的帖子排队等),或者当队列已满指定发件人应遵循的政策(即它必须等待,或将其丢弃请求并继续?)。我的问题是在何ASIO ::服务来设置这些功能?

Suppose I have a thread running an asio::service and a timer posting each 10ms a task to this thread. The task reception is bound to a method invokation which will make the thread sleeping for 100ms. I have therefore a timer posting 100 tasks a second to a thread which is capable of performing 10 tasks each second. It is evident that this situation will diverge. However, when dealing with queues, there are usually means to dimension the queue deepness (100? 1000? posts enqueued, etc.) or to specify the policy a sender should follow when the queue is full (i.e. shall it wait or shall it drop the request and continue?). My question is Ho to set these features in asio::service?

推荐答案

短耳不提供策略控制内部数据结构。但是,它确实提供了挂接到处理程序分配: asio_handler_allocate 和<一个href=\"http://www.boost.org/doc/libs/1_50_0/doc/html/boost_asio/reference/asio_handler_deallocate.html\"相对=nofollow> asio_handler_deallocate 。这些钩可被应用程序用于限制未完成的异步操作的量,以及限定在达到用户指定的限制时的行为。

Asio does not provide policies to control the internal data structures. However, it does provide hooks into handler allocations: asio_handler_allocate and asio_handler_deallocate. These hooks can be used by an application to limit the amount of outstanding asynchronous operations, as well as defining the behavior when the user specified limit is reached.

有需要考虑几个关键点:

There are a few key points to consider:


  • asio_handler_allocate 预计将返回一个有效的内存块或抛出异常。如果一个异常是从 asio_handler_allocate 抛出,将继续通过像的 io_service对象::后() 。因此,当达到最大时,非阻塞的行为,投掷可能是唯一的选择。

  • 考虑在组成操作,如的 async_read ,其中 asio_handler_allocate asio_handler_deallocate 可称为多次。如果一个异常被抛出,线程的堆栈将放松到至少 io_service对象::运行被调用在该点。如果发生阻塞,那么它可能是可能有所有线程维修反应器被堵塞,基本上是$ P $无法完成pventing所有异步作业。

  • asio_handler_allocate is expected to return a valid memory block or throw an exception. If an exception is thrown from asio_handler_allocate, it will continue unwinding the stack through calls like io_service::post(). Thus, for a non-blocking behavior when the max is reached, throwing may be the only option.
  • Consider the effects on composed operations, such as async_read, where asio_handler_allocate and asio_handler_deallocate may be called multiple times. If an exception is thrown, a thread's stack will unwind to at least the point at which io_service::run was invoked. If blocking occurs, then it could be possible to have all threads servicing the reactor to become blocked, essentially preventing all asynchronous jobs from completing.

下面是一个分配例如被用于处理呈现出内存池Boost.Asio的例子。

Here is an allocation example from the Boost.Asio examples showing a memory-pool being used for handlers.

这篇关于提高ASIO服务队列深度和政策的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆