您的位置:程序门 -> vc/mfc -> 基础类



请教 关于io模型的问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


请教 关于io模型的问题
发表于:2007-08-02 00:32:56 楼主
在win系统下的各种异步io模型

由于我也缺乏这方面的实践经验,所以对于这些模型的诞生起因以及用途都不是很清楚

请问   完成端口模型   一般用于什么场合?   它的内部实现是不是不需要开多线程就可以完成指定的工作?

因为平时我在代码中,只要是涉及不能立刻完成的工作,都是开多线程解决了,例如界面(读写文件进行处理)或者是socket通信(多客户端与服务器通信)什么的。
  我就没有搞清楚   完成端口模型的优势在哪里,和多线程方案相比。     仅仅是不需要程序员自己管理多个线程这么简单?还是因为其它什么原因?

恳请诸位赐教,最好能说出它的机理最好。    

或者有什么   io模型方面的资料可以推荐的?  

我现在在看win32多线程程序设计,没太弄明白,上来问问
发表于:2007-08-02 09:47:111楼 得分:0
using   file,   named   pipe,   or   communications   device!always   using   read   and   write   cooperation   !
发表于:2007-08-02 09:48:402楼 得分:0
synchronization   and   overlapped   input   and   output
you   can   perform   either   synchronous   or   asynchronous   (or   overlapped)   i/o   operations   on   files,   named   pipes,   and   serial   communications   devices.   the   writefile,   readfile,   deviceiocontrol,   waitcommevent,   connectnamedpipe,   and   transactnamedpipe   functions   can   be   performed   either   synchronously   or   asynchronously.   the   readfileex   and   writefileex   functions   can   be   performed   asynchronously   only.


when   a   function   is   EXECuted   synchronously,   it   does   not   return   until   the   operation   has   been   completed.   this   means   that   the   EXECution   of   the   calling   thread   can   be   blocked   for   an   indefinite   period   while   it   waits   for   a   time-consuming   operation   to   finish.   functions   called   for   overlapped   operation   can   return   immediately,   even   though   the   operation   has   not   been   completed.   this   enables   a   time-consuming   i/o   operation   to   be   EXECuted   in   the   background   while   the   calling   thread   is   free   to   perform   other   tasks.   for   example,   a   single   thread   can   perform   simultaneous   i/o   operations   on   different   handles,   or   even   simultaneous   read   and   write   operations   on   the   same   handle.

to   synchronize   its   EXECution   with   the   completion   of   the   overlapped   operation,   the   calling   thread   uses   the   getoverlappedresult   function   or   one   of   the   wait   functions   to   determine   when   the   overlapped   operation   has   been   completed.   you   can   also   use   the   hasoverlappediocompleted   macro   to   poll   for   completion.

to   cancel   all   pending   asynchronous   i/o   operations,   use   the   cancelio   function.   this   function   only   cancels   operations   issued   by   the   calling   thread   for   the   specified   file   handle.

overlapped   operations   require   a   file,   named   pipe,   or   communications   device   that   was   created   with   the   file_flag_overlapped   flag.   to   call   a   function   to   perform   an   overlapped   operation,   the   calling   thread   must   specify   a   pointer   to   an   overlapped   structure   that   has   had   all   of   its   members   initialized   to   zero.   if   this   pointer   is   null,   the   function   return   value   may   incorrectly   indicate   that   the   operation   completed.   the   system   sets   the   state   of   the   event   object   to   nonsignaled   when   a   call   to   the   i/o   function   returns   before   the   operation   has   been   completed.   the   system   sets   the   state   of   the   event   object   to   signaled   when   the   operation   has   been   completed.

when   a   function   is   called   to   perform   an   overlapped   operation,   it   is   possible   that   the   operation   will   be   completed   before   the   function   returns.   when   this   happens,   the   results   are   handled   as   if   the   operation   had   been   performed   synchronously.   if   the   operation   was   not   completed,   however,   the   function 's   return   value   is   false,   and   the   getlasterror   function   returns   error_io_pending.

a   thread   can   manage   overlapped   operations   by   either   of   two   methods:


use   the   getoverlappedresult   function   to   wait   for   the   overlapped   operation   to   be   completed.  
specify   a   handle   to   the   overlapped   structure 's   manual-reset   event   object   in   one   of   the   wait   functions   and   then   call   getoverlappedresult   after   the   wait   function   returns.   the   getoverlappedresult   function   returns   the   results   of   the   completed   overlapped   operation,   and   for   functions   in   which   such   information   is   appropriate,   it   reports   the   actual   number   of   bytes   that   were   transferred.  
when   performing   multiple   simultaneous   overlapped   operations,   the   calling   thread   must   specify   an   overlapped   structure   with   a   different   manual-reset   event   object   for   each   operation.   to   wait   for   any   one   of   the   overlapped   operations   to   be   completed,   the   thread   specifies   all   the   manual-reset   event   handles   as   wait   criteria   in   one   of   the   multiple-object   wait   functions.   the   return   value   of   the   multiple-object   wait   function   indicates   which   manual-reset   event   object   was   signaled,   so   the   thread   can   determine   which   overlapped   operation   caused   the   wait   operation   to   be   completed.

if   no   event   object   is   specified   in   the   overlapped   structure,   the   system   signals   the   state   of   the   file,   named   pipe,   or   communications   device   when   the   overlapped   operation   has   been   completed.   thus,   you   can   specify   these   handles   as   synchronization   objects   in   a   wait   function,   though   their   use   for   this   purpose   can   be   difficult   to   manage.   when   performing   simultaneous   overlapped   operations   on   the   same   file,   named   pipe,   or   communications   device,   there   is   no   way   to   know   which   operation   caused   the   object 's   state   to   be   signaled.   it   is   safer   to   use   a   separate   event   object   for   each   overlapped   operation.

for   examples   that   illustrate   the   use   of   overlapped   operations,   completion   routines,   and   the   getoverlappedresult   function,   see   using   pipes.
发表于:2007-08-02 10:30:283楼 得分:0
这些是msdn上的使用说明啊,我看了

但是   它和多线程的解决方案相比,有什么优势或者劣势呢,这是我很想搞清楚的


快速检索

最新资讯
热门点击