The ASIO library provides one by one corresponding operations for both synchronous and asynchronous. Asynchronous support is based on the pre-camera design pattern. The advantages and disadvantages of this model will be described below, corresponding to the reflector mode, the mode is a synchronous mode.
The implementation of the front camera:
In the ASIO library, a proactive approach is implemented as follows, which is consistent across platforms.
Front-camera design mode:
One, asynchronous operation
Defines an asynchronous execution of an operation, such as asynchronous read and write on a socket.
Second, asynchronous operation processing
Executes an asynchronous operation and, when the operation finishes, executes an event on the event completion queue. At a higher level, services such as Stream_socket_service are asynchronous operation processors.
Iii. Event Completion Queue
Stores the completion events until these events are out of the queue, and the asynchronous event splitter will boot the event out of the queue.
Iv. completion of the processing function
The handler function is used to handle the return result of an asynchronous operation. are function objects and are implemented using Boost::bind.
V. Asynchronous event Separators
The asynchronous event splitter will block waiting for the asynchronous completion event queue to generate a new event and return a completed event to the caller.
Six, the front camera
Call the asynchronous event splitter to make the event out of the queue and register the event handler. The abstract design typically represents classes such as Io_service.
Vii. Caller
The asynchronous operation is initiated by the application caller. The caller interacts with the asynchronous manipulation processor through the upper interface, such as Basic_stream_socket,
The upper interface, in turn, delegates a service such as Stream_socket_service to handle the interaction.
The realization of the reflector:
Many platforms, the ASIO library uses reflectors to implement the design patterns of the front camera, such as I/O multiplexing functions such as SELECT, Epoll, or Kqueue. These
The following areas are implemented similar to the design pattern of the front camera:
One, asynchronous operation processor
The reflector is implemented using functions such as SELECT, Epoll, or Kequeue. When a reflector indicates that a resource is ready to perform an operation, the processor performs an asynchronous operation and inserts the related completion event handler into the completion event queue.
Second, complete the event queue
The completed event queue is a linked list of multiple callback functions that handle completion events.
Three, asynchronous event separators
The asynchronous event splitter is implemented by waiting for an event to occur or changing the condition variable and can get the completion event handler.
Windows system overrides I/O operations
On the Windows nt,2000 and XP operating systems, the ASIO library provides an efficient proactive implementation using overloaded I/O. This pattern of implementation
Roughly as follows:
One, asynchronous operation processor
This process is implemented by the operating system, and various operations are initialized with overloaded functions such as Aceptex.
Second, complete the event queue
The implementation is done by the operating system and is related to the I/O port, and there is an I/O completion port for each Io_service instance.
Three, asynchronous event separators
Used by ASIO to perform event-out operations, all of which are bound to the event handler function.
Advantage:
First, portability.
Many operating systems provide native asynchronous I/O interfaces (such as the I/O overloaded interfaces above windows), which may be implemented with native asynchronous I/O. However, if native support is not available, these asynchronous libraries can be implemented using synchronous event separators as a typical reflector application, such as
POSIX Select function.
Second, concurrent multithreading
Long duration Operations exhibit asynchronous operability, and an application is a typical representation. Therefore, the application does not need to call too many threads to increase concurrency.
Iii. Performance and Scalability
Using each thread to process a single connection will degrade system performance, mainly due to increased context switching, synchronization processing, and data migration. Takes an asynchronous operation to avoid context switching consumption by minimizing the number of operating system threads and activating only the logical thread that handles the event.
Lesson Two: The design pattern of the front camera (not using multithreading concurrency)