http://www.artima.com/articles/io_design_patterns.html
转载自:http://www.cppblog.com/kevinlynx/archive/2008/06/06/52356.html
Proactor和Reactor模式_继续并发系统设计的扫盲
6.6.2008@H_403_11@
Kevin Lynx@H_403_11@
Proactor和Reactor都是并发编程中的设计模式。在我看来,他们都是用于派发/分离IO操作事件的。这里所谓的
IO事件也就是诸如read/write的IO操作。"派发/分离"就是将单独的IO事件通知到上层模块。两个模式不同的地方
在于,Proactor用于异步IO,而Reactor用于同步IO。@H_403_11@
摘抄一些关键的东西:@H_403_11@
"
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.
"@H_403_11@
关于两个模式的大致模型,从以下文字基本可以明白:@H_403_11@
"
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:@H_403_11@
* 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 .@H_403_11@
By comparison,here is a read operation in Proactor (true async):@H_403_11@
* 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.@H_403_11@
"@H_403_11@
可以看出,两个模式的相同点,都是对某个IO事件的事件通知(即告诉某个模块,这个IO操作可以进行或已经完成)。在结构
上,两者也有相同点:demultiplexor负责提交IO操作(异步)、查询设备是否可操作(同步),然后当条件满足时,就回调handler。
不同点在于,异步情况下(Proactor),当回调handler时,表示IO操作已经完成;同步情况下(Reactor),回调handler时,表示
IO设备可以进行某个操作(can read or can write),handler这个时候开始提交操作。@H_403_11@
用select模型写个简单的reactor,大致为:@H_403_11@
@H_403_11@
@H_403_11@class@H_403_11@handler
{
public@H_403_11@:
virtual@H_403_11@void@H_403_11@onRead()=0;
void@H_403_11@onWrite()=0;
void@H_403_11@onAccept()=0;
}@H_403_11@;
class@H_403_11@dispatch
{
void@H_403_11@poll()
{
//@H_403_11@addfdintheset.
@H_403_11@
@H_403_11@polleveryfd@H_403_11@int@H_403_11@c=select(0,&read_fd,&write_fd,0);
if@H_403_11@(c>0)
{
for@H_403_11@eachfdin@H_403_11@theread_fd_set
{if@H_403_11@fdcanread
_handler->onRead();
if@H_403_11@fdcanaccept
_handler->onAccept();
}@H_403_11@
in@H_403_11@thewrite_fd_set
{
if@H_403_11@fdcanwrite
_handler->onWrite();
}@H_403_11@
}@H_403_11@
}@H_403_11@
void@H_403_11@setHandler(handler*_h)
{
_handler=_h;
}@H_403_11@
private@H_403_11@:
handler*_handler;
}@H_403_11@;
///@H_403_11@application@H_403_11@@H_403_11@@H_403_11@
class@H_403_11@MyHandler: public@H_403_11@handler
{
void@H_403_11@onRead()
{
}@H_403_11@
void@H_403_11@onWrite()
{
}@H_403_11@
void@H_403_11@onAccept()
{
}@H_403_11@
}@H_403_11@;
在网上找了份Proactor模式比较正式的文档,其给出了一个总体的UML类图,比较全面:@H_403_11@
class@H_403_11@AsyIOProcessor
{
void@H_403_11@do_read()
{
sendreadoperationtoOS
@H_403_11@readiofinished.anddispatchnotification@H_403_11@
@H_403_11@_proactor->dispatch_read();
}@H_403_11@
private@H_403_11@:
Proactor*_proactor;
}@H_403_11@;
class@H_403_11@Proactor
{
void@H_403_11@dispatch_read()
{
_handlerMgr->onRead();
}@H_403_11@
private@H_403_11@:
HandlerManager*_handlerMgr;
}@H_403_11@;
class@H_403_11@HandlerManager
{
public@H_403_11@:
typedefstd::list<Handler*>HandlerList;
void@H_403_11@onRead()
{
notifyallthehandlers.@H_403_11@
@H_403_11@std::for_each(_handlers.begin(),_handlers.end(),onRead);
}@H_403_11@
private@H_403_11@:
HandlerList*_handlers;
}@H_403_11@;
class@H_403_11@Handler
{
void@H_403_11@onRead()=0;
}@H_403_11@;
//@H_403_11@
applicationlevelhandler.@H_403_11@
@H_403_11@
public@H_403_11@Handler
{
void@H_403_11@onRead()
{
@H_403_11@
@H_403_11@}@H_403_11@
}@H_403_11@;