Reactor Reactor mode ----- single multi-threaded and multi-threaded master-slave Reactor

Single Reactor Multithreading:

, Reactor runs in a single thread for a single Reactor multi-threaded model. High concurrency scenarios easily become a performance bottleneck, allowing Reactor run in multiple threads

Thread model:

Here Insert Picture Description

Method Description:

1.Reactor Object Request select events by monitoring client, after receiving the event, distributed through dispatch
2. If the connection request, the connection request is processed through the right Acceptor accept, and then create a Handler object handles a variety of events after the connection is completed
3. If it is not a connection request by calling Reactor distribution corresponding to the connection handler to handle
4.handler only responsible for the corresponding event, not a specific business process, data is read by the rad will be distributed to a worker thread pool behind business process thread
5 worker thread pool is allocated a separate thread, complete real business, while the result of the processing handler returns to
the handler. 6 receives the response and send the results back to the client through the
advantages:
can take advantage of multi-core CPU processing capabilities
disadvantages:
multiple threads can share and access data more complex, reactor handled listen to all the events and the corresponding, in single thread, prone to performance bottlenecks in high concurrency scenarios

Master-slave Reactor multithreading:

, Reactor runs in a single thread for a single Reactor multi-threaded model. High concurrency scenarios easily become a performance bottleneck, allowing Reactor run in multiple threads.

Thread model:

Here Insert Picture Description

plan description:

1.Reactor主线程MainReactor对象通过select监听连接事件,收到事件后,通过Acceptor处理连接事件
2.当Acceptor处理连接事件后,MainReactor将连接分配给SubReactor
3.subReactor将连接加入到连接队列进行监听,并创建handler进行各种事件处理
4.当有新的事件发生时,subReactor就会调用对应的handler进行处理
5.handler通过rand读取数据,会分发给后面的worker线程进行处理
6.wroker线程池会分独立的worker线程进行业务处理,并返回结果
7.handler收到响应的结果后,再通过sned将结果返回给client
8.Reactor主线程可以对应多个Reactor子线程,即MainReactor可以关联多个subReactor。
优点:
父线程与子线程的数据交互简单,职责明确,父线程只需要接收新连接,子线程完成后续的业务处理
父线程与子线程的数据交互简单,Reactor主线程只需要把新连接传给子线程,子线程无需返回数据
缺点:
编程复制读较高。

Reactor模式具有如下优点:

1.响应快,不必为单个同步时间所阻塞,虽然Reactor本身依然是同步的
2.可以最大程度的避免复杂的多线程以及同步问题,避免了多线程/进程的切换开销
3.扩展性好,可以方便的通过Reactor实例个数来充分的利用CPU的资源
4.复用性好,Reactor模型本身与具有事件处理逻辑无关,具有很高的复用性

学习年限不足,知识过浅,说的不对请见谅。

There are 10 kinds of people in the world, one is to understand binary, one is do not understand binary.

Published 71 original articles · won praise 54 · views 420 000 +

Guess you like

Origin blog.csdn.net/weixin_43326401/article/details/104202424