Reactor Model
The reactor mode is a common mode for processing concurrent I/O. The central idea is to register all the I/O events to be processed on a central I/O multiplexing, at the same time, the main thread is congested on the multiplex; once an I/O event arrives or is ready (the difference is whether the Multiplex is edge-triggered or horizontal-triggered ), the multiplexing returns and distributes the corresponding I/O events to the corresponding processor.
There are three important components:
- Multiplexing: it is provided by the operating system and generally called by select, poll, epoll and other systems in Linux.
- Event distributor: divides the ready events returned from the multiplexing into corresponding processing functions.
- Event processor: The processing function that processes specific events.
Because this model is often used, many people encapsulate simple system calls to form a cross-platform event processing library. Typical examples include libevent, libev, and boost.
ASIO.
Proactor Model
In contrast to the reactor model, the biggest feature of proactor is the use of asynchronous I/O. All I/O operations are performed by the asynchronous I/O interface provided by the system. The proactor multiplexing waits for asynchronous I/O to complete and calls the corresponding user processing function. To compare the reactor model, a read operation is used as an example:
In reactor:
- The file descriptor to be read is registered in the multiplexing.
- The multiplexing waits for the readable event of the descriptor and all other registered events.
- After the descriptor is readable, The multipleth interface returns and calls the processing function provided by the user to start File Read operations.
In proactor:
- User functions start an asynchronous file read operation. At the same time, register this operation on the multiplex. The multiplexing does not care about whether the file is readable, but whether the asynchronous read operation is complete.
- Asynchronous file reading is completed by the operating system, and user programs do not need to be concerned. The multiplexing waits until there is a notification of completion.
- After the operating system completes the Read File Operation and copies the read data to the buffer zone provided by the user, it notifies the multiplexing that the read operation has been completed.
- The multiplexing then calls the corresponding processing program to process the data.