{"rsdb":{"rid":"2859","subhead":"","postdate":"0","aid":"1243","fid":"56","uid":"1","topic":"1","content":"

\u5176\u5b9e\u8fd9\u56db\u4e2a\u540d\u8bcd\u5728\u6982\u5ff5\u4e0a\u6709\u4e00\u4e9b\u5fae\u5999\u7684\u5dee\u522b\uff0c<\/font>
Sync\/Async IO\u7684\u5dee\u522b\u5728\u4e8e\u7a0b\u5f0f\u662f\u5426\u8981\u53bb\u7b49\u5f85OS\u7684IO\u64cd\u4f5c\u7ed3\u675f\uff1a\u5728boost\u4e2d\u662f\u6307io_service\u9009\u7528\u4e86run()\u6216\u8005\u662fpoll()\uff0c\u4f1a\u8fdb\u884ckernel context switch\u5230OS kernel\u6765\u8fd0\u884c\u3002<\/font>
\u800cBlocking\/Non-Blocking\u5219\u662f\u6307\u6211\u4eec\u662f\u5426\u8981\u7b49\u5f85\u7a0b\u5f0f\u6267\u884c\u6d41\u7a0b\u4e2d\u7684IO\u7ed3\u675f\u624d\u80fd\u7ee7\u7eed\u6267\u884c\uff0c\u4e3b\u8981\u662f\u7531application\u6765\u5b9e\u4f5c\u3001\u8bbe\u8ba1\u6267\u884c\u7684\u673a\u5236\u3002<\/font><\/p>

\u5176\u5b9e\u4e00\u4e2a\u7a0b\u5f0f\u662f\u4e0d\u662fSync\/Asyn\u6216blocking\/non-blocking\uff0c\u5176\u5b9e\u770b\u7684\u662f\u4e00\u6bb5\u7a0b\u5f0f\u7801\u6216\u8005\u662f\u4e00\u4e2afunction<\/font><\/p>

\u901a\u5e38\u5982\u679c\u8be5\u6bb5\u7a0b\u5f0f\u7801\u5305\u542bwhile\u56de\u5708\u6765\u5224\u65ad\u67d0\u4e2a\u6863\u6848\u662f\u72b6\u6001\uff0c\u90a3\u4e48\u5c31\u662fblocking\uff0c\u4f46\u5982\u679c\u8fd9\u65f6\u5019\u53c8\u7a0b\u5f0fthread\u6765\u5206\u914d\u8be5\u4efb\u52a1\u7684\u6267\u884c\uff0c\u90a3\u4e48\u5c31\u662fnon-blocking\u3002<\/font><\/p>

\u5728non-blocking\u7684\u7a0b\u5f0f\u4e2d\uff0c\u5982\u679c\u6709\u7275\u626f\u5230\u6863\u6848\u7684\u8bfb\u5199\u95ee\u9898\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u5fc5\u987b\u5728\u7a0b\u5f0f\u4e2d\u64b0\u5199\u4e00\u4e9b\u673a\u5236\u53bb\u786e\u8ba4\u8be5\u6863\u6848\u662f\u5982\u5df2\u7ecf\u53ef\u4ee5\u5199\u5165\uff0c\u4ee5\u5fc5\u514d\u9020\u6210race condition\u7684\u60c5\u51b5\u3002<\/font>\u901a\u5e38\u4f9d\u7167\u4e0d\u540c\u7684\u4f5c\u4e1a\u7cfb\u7edf\u4f1a\u4f7f\u7528poll\u3001select\u3001epoll\u3001iocp\u7b49\u673a\u5236\u3002<\/font><\/font><\/p>


<\/font><\/font><\/p>

\u5728boost::ASIO\u4e2d\uff0c\u7531\u4e8e\u4e3a\u4e86\u8981\u8fbe\u6210\u8de8\u5e73\u53f0\uff0c\u56e0\u6b64\u5728\u80cc\u540e\u5b9e\u4f5c\u4e86Proactor\u6a21\u5f0f\u6765\u9690\u85cf\u7ec6\u8282\uff0c\u5e76\u4ee5Reactor\u6765\u5b9e\u4f5c\u4e0d\u540c\u5e73\u53f0\u7684\u673a\u5236\u3002<\/font>\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u4ee5\u4e0b\u7684\u7a0b\u5f0f\u7801\u5370\u51fa\u6211\u4eec\u7684\u4f5c\u4e1a\u7cfb\u7edf\u6240\u91c7\u7528\u7684\u662f\u90a3\u4e00\u79cd\uff1a<\/font><\/font><\/p>

<\/font><\/font><\/span><\/a><\/sup><\/span>View Code<\/font><\/font><\/a> CPP<\/font><\/font><\/span>
 <\/div><\/div>
1<\/font><\/font>\r\n2<\/font><\/font>\r\n3<\/font><\/font>\r\n4<\/font><\/font>\r\n5<\/font><\/font>\r\n6<\/font><\/font>\r\n7<\/font><\/font>\r\n8<\/font><\/font>\r\n9<\/font><\/font>\r\n10<\/font><\/font>\r\n11<\/font><\/font>\r\n12<\/font><\/font>\r\n13<\/font><\/font>\r\n14<\/font><\/font>\r\n15<\/font><\/font>\r\n16<\/font><\/font>\r\n17<\/font><\/font>\r\n18<\/font><\/font>\r\n19<\/font><\/font>\r\n20<\/font><\/font>\r\n<\/font><\/font> \r\n#include <iostream> <\/font><\/font><\/span>\r\n#include <string> <\/font><\/font><\/span>\r\n#include <boost \/asio.hpp> <\/font><\/font><\/span>\r\nint<\/font><\/font><\/span> main <\/font><\/font>( <\/font><\/font><\/span>) <\/font><\/font><\/span> \r\n{<\/font><\/font><\/span> \r\nstd <\/font><\/font>:: <\/font><\/font><\/span>string<\/font><\/font><\/span> output <\/font><\/font>; <\/font><\/font><\/span> \r\n#if defined(BOOST_ASIO_HAS_IOCP)<\/font><\/font><\/span> \r\n output <\/font><\/font>= <\/font><\/font><\/span> \"iocp\" <\/font><\/font><\/span> ; <\/font><\/font><\/span> \r\n#elif defined(BOOST_ASIO_HAS_EPOLL)<\/font><\/font><\/span> \r\n output <\/font><\/font>= <\/font><\/font><\/span> \"epoll\" <\/font><\/font><\/span> ; <\/font><\/font><\/span> \r\n#elif defined(BOOST_ASIO_HAS_KQUEUE)<\/font><\/font><\/span> \r\n output <\/font><\/font>= <\/font><\/font><\/span> \"kqueue\" <\/font><\/font><\/span> ; <\/font><\/font><\/span> \r\n#elif defined(BOOST_ASIO_HAS_DEV_POLL)<\/font><\/font><\/span> \r\n output <\/font><\/font>= <\/font><\/font><\/span> \"\/dev\/poll\" <\/font><\/font><\/span> ; <\/font><\/font><\/span> \r\n#else<\/font><\/font><\/span> \r\n output <\/font><\/font>= <\/font><\/font><\/span> \"select\" <\/font><\/font><\/span> ; <\/font><\/font><\/span> \r\n#endif<\/font><\/font><\/span> \r\n std <\/font><\/font>:: <\/font><\/font><\/span>cout <\/font><\/font><\/span> < <\/font><\/font><\/span> <<\/font><\/font><\/span> output <\/font><\/font><<<\/font><\/font><\/span> std <\/font><\/font>:: <\/font><\/font><\/span>endl <\/font><\/font><\/span>; <\/font><\/font><\/span> \r\n}<\/font><\/font><\/span><\/td><\/font><\/font><\/font><\/font><\/font>

 <\/p><\/font>


<\/div><\/font><\/font><\/font><\/font><\/font><\/font>","orderid":"0","title":"ASIO\u5b66\u4e60\u7b14\u8bb0\uff1aSync\/Async\u3001Blocking\/Non-Blocking IO","smalltitle":"","mid":"0","fname":"\u7f51\u7edc\u7f16\u7a0b","special_id":"0","bak_id":"0","info":"0","hits":"2312","pages":"1","comments":"0","posttime":"2012-09-22 13:47:11","list":"1348292831","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"http:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"ASIO<\/A> \u5b66\u4e60<\/A> \u7b14\u8bb0<\/A> Sync\/Async<\/A> Blocking\/Non-Blocking<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"220.113.167.153","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"\u5176\u5b9e\u8fd9\u56db\u4e2a\u540d\u8bcd\u5728\u6982\u5ff5\u4e0a\u6709\u4e00\u4e9b\u5fae\u5999\u7684\u5dee\u522b\uff0cSync\/Async IO\u7684\u5dee\u522b\u5728\u4e8e\u7a0b\u5f0f\u662f\u5426\u8981\u53bb\u7b49\u5f85OS\u7684IO\u64cd\u4f5c\u7ed3\u675f\uff1a\u5728boost\u4e2d\u662f\u6307io_service\u9009\u7528\u4e86run()\u6216\u8005\u662fpoll()\uff0c\u4f1a\u8fdb\u884ckernel context switch\u5230OS kernel\u6765\u8fd0\u884c\u3002\u800cBlocking\/Non-Blocking\u5219\u662f\u6307\u6211\u4eec\u662f\u5426\u8981\u7b49\u5f85\u7a0b\u5f0f\u6267\u884c\u6d41\u7a0b\u4e2d..","lastview":"1500590437","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"\u6b20\u6241|1~~|1","htmlname":"","city_id":"0"},"page":"1"}