All of these books are published by Wiley. This book represents the progression and evolution of the pattern approach into a system of patterns capable of describing and documenting large-scale applications. The patterns presented in this book span several levels of abstractions from high-level architectural patterns and medium-level design patterns to low-level idioms. The 17 patterns in POSA2 form the basis of a pattern language that addresses issues associated with concurrency and networking.
|Published (Last):||15 October 2008|
|PDF File Size:||3.35 Mb|
|ePub File Size:||18.23 Mb|
|Price:||Free* [*Free Regsitration Required]|
Let us examine how the Proactor design pattern is implemented in Boost. Asio, without reference to platform-specific details.
Proactor design pattern adapted from [POSA2] — Asynchronous Operation Defines an operation that is executed asynchronously, such as an asynchronous read or write on a socket.
These are function objects, often created using boost::bind. On many platforms, Boost. Asio implements the Proactor design pattern in terms of a Reactor, such as select, epoll or kqueue. This implementation approach corresponds to the Proactor design pattern as follows: — Asynchronous Operation Processor A reactor implemented using select, epoll or kqueue. When the reactor indicates that the resource is ready to perform the operation, the processor executes the asynchronous operation and enqueues the associated completion handler on the completion event queue.
This implementation approach corresponds to the Proactor design pattern as follows: — Asynchronous Operation Processor This is implemented by the operating system. Operations are initiated by calling an overlapped function such as AcceptEx. However, if native support is not available, the library may also be implemented using synchronous event demultiplexors that typify the Reactor pattern, such as POSIX select.
Long-duration operations are performed asynchronously by the implementation on behalf of the application. Consequently applications do not need to spawn many threads in order to increase concurrency. Implementation strategies such as thread-per-connection which a synchronous-only approach would require can degrade system performance, due to increased context switching, synchronisation and data movement among CPUs. With asynchronous operations it is possible to avoid the cost of context switching by minimising the number of operating system threads — typically a limited resource — and only activating the logical threads of control that have events to process.
Asynchronous operation completion handlers can be written as though they exist in a single-threaded environment, and so application logic can be developed with little or no concern for synchronisation issues. Function composition refers to the implementation of functions to provide a higher-level operation, such as sending a message in a particular format. Each function is implemented in terms of multiple calls to lower-level read or write operations.
For example, consider a protocol where each message consists of a fixed-length header followed by a variable length body, where the length of the body is specified in the header.
To compose functions in an asynchronous model, asynchronous operations can be chained together. That is, a completion handler for one operation can initiate the next. Starting the first call in the chain can be encapsulated so that the caller need not be aware that the higher-level operation is implemented as a chain of asynchronous operations.
The ability to compose new operations in this way simplifies the development of higher levels of abstraction above a networking library, such as functions to support a specific protocol. Disadvantages — Program complexity. It is more difficult to develop applications using asynchronous mechanisms due to the separation in time and space between operation initiation and completion. Applications may also be harder to debug due to the inverted flow of control.
Buffer space must be committed for the duration of a read or write operation, which may continue indefinitely, and a separate buffer is required for each concurrent operation. The Reactor pattern, on the other hand, does not require buffer space until a socket is ready for reading or writing. Wiley,