actor、reactor與proactor模型:高性能服務器的幾種模型概念。
actor模型:
實體之通過消息通訊,各自處理自己的數據,能夠實現這並行。
說白了,有點像rpc。
skynet是actor模型。
reactor模型:
1 向事件分發器註冊事件回調
2 事件發生
4 事件分發器調用之前註冊的函數
4 在回調函數中讀取數據,對數據進行後續處理
libevent是reactor模型。
proactor模型:
1 向事件分發器註冊事件回調
2 事件發生
3 操作系統讀取數據,並放入應用緩衝區,然後通知事件分發器
4 事件分發器調用之前註冊的函數
5 在回調函數中對數據進行後續處理
ASIO是preactor模型。
reactor和proactor的主要區別是,前者應用在回調函數中讀取數據,然後進行後續的數據處理;而後者數據讀取有操作系統完成,回調函數製作數據處理。Proactor是異步,Reactor是同步阻塞。
相同點:demultiplexor負責提交IO操作(異步)、查詢設備是否可操作(同步),然後當條件滿足時,就回調handler。
不同點:
- 異步情況下(Proactor),當回調handler時,表示IO操作已經完成;
- 同步情況下(Reactor),回調handler時,表示IO設備可以進行某個操作(can read or can write),handler這個時候開始提交操作。
摘抄一些關鍵的東西:
"
Two patterns that involve event demultiplexors are called Reactor and Proactor [1]. The Reactor patterns
involve synchronous I/O, whereas the Proactor pattern involves asynchronous I/O.
"
關於兩個模式的大致模型,從以下文字基本可以明白:
"
An example will help you understand the difference between Reactor and Proactor. We will focus on the read
operation here, as the write implementation is similar. Here's a read in Reactor:
* An event handler declares interest in I/O events that indicate readiness for read on a particular socket ;
* The event demultiplexor waits for events ;
* An event comes in and wakes-up the demultiplexor, and the demultiplexor calls the appropriate handler;
* The event handler performs the actual read operation, handles the data read, declares renewed interest in
I/O events, and returns control to the dispatcher .
By comparison, here is a read operation in Proactor (true async):
* A handler initiates an asynchronous read operation (note: the OS must support asynchronous I/O). In this
case, the handler does not care about I/O readiness events, but is instead registers interest in receiving
completion events;
* The event demultiplexor waits until the operation is completed ;
* While the event demultiplexor waits, the OS executes the read operation in a parallel kernel thread, puts
data into a user-defined buffer, and notifies the event demultiplexor that the read is complete ;
* The event demultiplexor calls the appropriate handler;
* The event handler handles the data from user defined buffer, starts a new asynchronous operation, and returns
control to the event demultiplexor.