简体   繁体   English

在几个线程上运行asio strand和io_service

[英]Boost asio strand and io_service running on several threads

I am not sure about one detail related to strands. 我不确定与股线有关的一个细节。

Suppose the following situation: two independent objects each one with his own strand. 假设以下情况:两个独立的对象,每个对象都有自己的链。 And each strand related to one common io_service. 并且每个链与一个共同的io_service相关。 Each object use his strand for posting and wrapping async operations. 每个对象都使用他的strand来发布和包装异步操作。 If I have this (unique) io_service .run()'ing on several threads, I am not sure if the following will happen: 如果我在多个线程上有这个(唯一的)io_service .run(),我不确定是否会发生以下情况:

  1. All operations posted and async wrapped by one of the objects will be executed non concurrently . 其中一个对象发布的所有操作和异步包装将不同时执行。 So all operations related to one of the objects will be executed serially (Posted operations will be executed in the same order than they were posted. Wrapped async operations will be executed in an unspecified order because they are asynchronous but still being executed serially). 因此,与其中一个对象相关的所有操作都将按顺序执行(已发布的操作将按照与发布时相同的顺序执行。包装的异步操作将以未指定的顺序执行,因为它们是异步的,但仍然是串行执行的)。

  2. Two operations originated in different objects (and therefore posted or wrapped from different strand objects related to the same io_service) could be executed concurrently . 两次手术起源于不同的对象 (因此发布或与同一io_service对象不同链的对象包装),可以同时执行。

  3. In summary, each object will execute his posted and wrapped handlers serially but handlers posted and wrapped from different objects (strands) will execute concurrently. 总之,每个对象将连续执行其发布和包装的处理程序,但是从不同对象(链)发布和包装的处理程序将同时执行。

      +-----------------+ +-----------------+ | Obj1 | | Obj2 | | +-------------+ | | +-------------+ | | | Strand_1 | | | | Strand_2 | | | +-------------+ | | +-------------+ | +--------+--------+ +-------+---------+ | | +--------+ +-------+ | | +----+--+----+ | io_service | +------------+ | | +--------+-------+ | | Thread1 Thread_2 io_service.run() io_service.run() 

Am I right? 我对吗?

Thank you 谢谢

In short, a strand guarantees sequential invocation of its own handlers, but makes no guarantee of concurrent execution of handlers from different strands. 简而言之,一个strand保证了对其自己的处理程序的顺序调用,但不保证从不同的链中并发执行处理程序。 Thus, the answers to the points are: 因此,这些要点的答案是:

  1. Yes. 是。 Sequential invocation is guaranteed. 顺序调用是有保证的。
  2. Yes. 是。 Concurrent execution could happen, but there is no guarantee. 并发执行可能会发生,但无法保证。
  3. Yes to sequential invocation, but no to the the guarantee that concurrent execution will occur. 是顺序调用,但不保证并发执行发生。

A strand maintains its own handler queue, and guarantees that only one of its handlers is in the io_service , resulting in handlers being synchronized before being placed into the io_service . 一个strand维护自己的处理程序队列,并保证只有一个处理程序在io_service ,导致处理程序在被放入io_service之前被同步。 Thus, all handlers posted or dispatched through a strand will be executed sequentially. 因此,通过strand发布或发送的所有处理程序将按顺序执行。

Concurrent execution of handlers posted or dispatched through different strand s can occur, it is just not guaranteed to occur. 可以发生通过不同strand发布或分派的处理程序的并发执行,但不能保证发生。 The documentation states: 文件说明:

The implementation makes no guarantee that handlers posted or dispatched through different strand objects will be invoked concurrently. 该实现不保证将同时调用通过不同strand对象发布或分派的处理程序。

Therefore, if Thread1 is executing a handler posted through Strand_1 , Boost.Asio will not use that information to guarantee that a handler posted through Strand_2 will be executed by Thread2 ; 因此,如果Thread1正在执行通过Strand_1发布的处理程序,Boost.Asio将不会使用该信息来保证通过Strand_2发布的处理程序将由Thread2执行; however, it is possible that Thread2 is selected to execute the handler from Strand_2 based on other implementation details, such as being the next available thread in the list of threads running the io_service . 但是,它是可能的, Thread2被选中执行从处理器Strand_2基于其它实施细节,如正在运行的线程列表中的下一个可用线程io_service

For example, consider the case where 3 handlers A , B , and C are ready to run within the io_service : 例如,考虑3个处理程序ABC准备好在io_service运行的情况:

  • A was posted posted through Strand_1 . A是通过Strand_1发布的。
  • B was not posted through a strand . B不是通过贴strand
  • C was posted through Strand_2 . C是通过Strand_2发布的。

If Thread1 and Thread2 are running the io_service , then one possible execution order is: 如果Thread1Thread2正在运行io_service ,那么一个可能的执行顺序是:

Thread1         | Thread2
----------------+----------------
start A()       | start B()
`-- finish A()  | |
start C()       | `-- finish B()
`-- finish C()  |

The illustrated execution order shows that that handlers ( A and C ) posted through different strand s ( Strand_1 and Strand_2 respectively) are not guaranteed to be executed concurrently. 所示的执行顺序显示通过不同的strand (分别为Strand_1Strand_2 )发布的处理程序( AC )不能保证同时执行。

All operations posted and async wrapped by one of the objects will be executed non concurrently. 由其中一个对象发布的所有操作和异步包装将不同时执行。 So all operations related to one of the objects will be executed serially (Posted operations will be executed in the same order than they were posted. Wrapped async operations will be executed in an unspecified order because they are asynchronous but still being executed serially). 因此,与其中一个对象相关的所有操作都将按顺序执行(已发布的操作将按照与发布时相同的顺序执行。包装的异步操作将以未指定的顺序执行,因为它们是异步的,但仍然是串行执行的)。

Yes. 是。

Two operations originated in different objects (and therefore posted or wrapped from different strand objects related to the same io_service) could be executed concurrently. 可以同时执行源自不同对象的两个操作(因此从与相同io_service相关的不同链对象发布或包装)。

Yes

In summary, each object will execute his posted and wrapped handlers serially but handlers posted and wrapped from different objects (strands) will execute concurrently. 总之,每个对象将连续执行其发布和包装的处理程序,但是从不同对象(链)发布和包装的处理程序将同时执行。

Yes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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