93

Reactor pattern is explained in wikipedia, and it's a bit too abstract. Can you describe this pattern in a more concrete way? Ideally with code snippets or high-level class diagrams describing some applications of reactor pattern.

Yasas Gunarathne
  • 833
  • 1
  • 6
  • 19
Eleco
  • 3,194
  • 9
  • 31
  • 40
  • 3
    Found this question to be a great answer - http://stackoverflow.com/questions/9138294/what-is-the-different-between-event-driven-model-and-reactor-pattern – Ryan Gibbons Apr 02 '13 at 23:09

2 Answers2

40

You might want to check the original paper describing it http://www.dre.vanderbilt.edu/~schmidt/PDF/reactor-siemens.pdf

The Reactor design pattern handles service requests that are delivered concurrently to an application by one or more clients. Each service in an application may consist of serveral methods and is represented by a separate event handler that is responsible for dispatching service-specific requests. Dispatching of event handlers is performed by an initiation dispatcher, which manages the registered event handlers. Demultiplexing of service requests is performed by a synchronous event demultiplexer.

reese
  • 726
  • 8
  • 12
  • 1
    As you'll notice when reading the article, Douglas Schmidt et.al implemented a highly efficient and modular C++ framework called The Adaptive Communications Environment, where the Reactor pattern plays a central role. The framework itself makes use of a plethora of design patterns and is worth examining for that sake alone. If you look for a portable framework for building highly scalable C++ backends, then ACE is worth taking a look at. – user2015735 Jun 03 '18 at 16:59
  • 1
    @reese Link is broken today man :( – Allan Chua Oct 01 '18 at 07:17
  • 1
    @AllanChua I think I found it - https://www.dre.vanderbilt.edu/~schmidt/PDF/Reactor.pdf – manish ma Dec 11 '18 at 14:50
  • 15
    OP asked for a "simple, concrete" explanation, and you offered something that is even more abstract than Wikipedia... – Zhe May 20 '20 at 01:30
23

A reactor allows multiple tasks which block (say due to IO) to be processed efficiently using a single thread. The reactor manages a pool of handlers and runs an event loop. When it is called to perform a task it links it with a new or vacant handler making it active. The event loop (1) finds all the handlers that are active and unblocked (or delegates this to a dispatcher implementation) (2) executes each of these found handlers sequentially until they either complete or reach a point where they block. Completed handlers become inactive and vacant for reuse whereas blocked active handlers yield, allowing the event loop to continue. (3) Repeats from step (1)

Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
andrew pate
  • 3,833
  • 36
  • 28
  • 1
    Downvote because this is not correct – clickMe Dec 20 '18 at 11:15
  • 4
    The core idea is to perform synchronous event demultiplexing. The event handlers are only called if they can execute in a non-blocking manner e.g. the whole data packet is available at a network socket, waiting for an event handler to process the data. This makes it possible to carry out the event handler sequentially in a non-blocking manner – clickMe Dec 20 '18 at 11:17
  • 2
    "the Reactor pattern is responsible for demultiplexing and dispatching of multiple event handlers that are triggered when it is possible to initiate an operation synchronously without blocking." from the paper linked in the accepted anwser – clickMe Dec 20 '18 at 11:52
  • This is not true at all. There as many threads in a non-reactor as there are in a reactor pattern. Instead of an event loop, you can have a single "driving" thread that doesn't use the observer/event-listener pattern. Same performance. – Zombies Jun 18 '20 at 13:24