锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲日韩中文字幕在线播放,亚洲午夜精品国产电影在线观看,国产成人精品亚洲2020http://m.tkk7.com/LittleDS/category/31585.htmlIf the only tool you have is a hammer, you tend to see every problem as a nail.zh-cnTue, 20 May 2008 02:28:54 GMTTue, 20 May 2008 02:28:54 GMT60Boost Thread瀛︿範(fàn)絎旇浜?/title><link>http://m.tkk7.com/LittleDS/archive/2008/05/18/201280.html</link><dc:creator>鏉ㄧ</dc:creator><author>鏉ㄧ</author><pubDate>Sun, 18 May 2008 10:32:00 GMT</pubDate><guid>http://m.tkk7.com/LittleDS/archive/2008/05/18/201280.html</guid><wfw:comment>http://m.tkk7.com/LittleDS/comments/201280.html</wfw:comment><comments>http://m.tkk7.com/LittleDS/archive/2008/05/18/201280.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/LittleDS/comments/commentRss/201280.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/LittleDS/services/trackbacks/201280.html</trackback:ping><description><![CDATA[澶氱嚎紼嬬紪紼嬩腑榪樻湁涓涓噸瑕佺殑姒傚康錛歍hread Local Store錛圱LS錛岀嚎紼嬪眬閮ㄥ瓨鍌級(jí)錛屽湪boost涓紝TLS涔熻縐頒綔TSS錛孴hread Specific Storage銆?br /> boost<strong><font color="#663300">::</font></strong>thread搴撲負(fù)鎴戜滑鎻愪緵浜?jiǎn)涓涓帴鍙g畝鍗曠殑TLS鐨勯潰鍚戝璞$殑灝佽錛屼互涓嬫槸tss綾葷殑鎺ュ彛瀹氫箟錛?br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #0000ff;">class</span><span style="color: #000000;"> tss<br /> {<br /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;">:<br />     tss(boost::function1</span><span style="color: #000000;"><</span><span style="color: #0000ff;">void</span><span style="color: #000000;">, </span><span style="color: #0000ff;">void</span><span style="color: #000000;">*>*</span><span style="color: #000000;"> pcleanup);<br />     </span><span style="color: #0000ff;">void</span><span style="color: #000000;">*</span><span style="color: #000000;"> get() </span><span style="color: #0000ff;">const</span><span style="color: #000000;">;<br />     </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> set(</span><span style="color: #0000ff;">void</span><span style="color: #000000;">*</span><span style="color: #000000;"> value);<br />     </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> cleanup(</span><span style="color: #0000ff;">void</span><span style="color: #000000;">*</span><span style="color: #000000;"> p);<br /> };</span></div> <br /> 鍒嗗埆鐢ㄤ簬鑾峰彇銆佽緗佹竻闄ょ嚎紼嬪眬閮ㄥ瓨鍌ㄥ彉閲忥紝榪欎簺鍑芥暟鍦ㄥ唴閮ㄥ皝瑁呬簡(jiǎn)TlsAlloc銆乀lsGetValue銆乀lsSetValue絳堿PI鎿嶄綔錛屽皢瀹冧滑灝佽鎴愪簡(jiǎn)OO鐨勫艦寮忋?br /> 浣哹oost灝嗚綾諱俊鎭皝瑁呭湪detail鍚嶅瓧絀洪棿鍐咃紝鍗充笉鎺ㄨ崘鎴戜滑浣跨敤錛屽綋闇瑕佷嬌鐢╰ss鏃訛紝鎴戜滑搴旇浣跨敤鍙︿竴涓嬌鐢ㄦ洿鍔犳柟渚跨殑綾伙細(xì)thread_specific_ptr錛岃繖鏄竴涓櫤鑳芥寚閽堢被錛岃綾葷殑鎺ュ彛濡備笅錛?font color="#990000"><br /> <br /> </font> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> thread_specific_ptr : </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> boost::noncopyable   </span><span style="color: #008000;">//</span><span style="color: #008000;"> Exposition only</span><span style="color: #008000;"><br /> </span><span style="color: #008080;"> 2</span> <span style="color: #000000;">{<br /> </span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">public</span><span style="color: #000000;">:<br /> </span><span style="color: #008080;"> 4</span> <span style="color: #000000;">  </span><span style="color: #008000;">//</span><span style="color: #008000;"> construct/copy/destruct</span><span style="color: #008000;"><br /> </span><span style="color: #008080;"> 5</span> <span style="color: #000000;">  thread_specific_ptr();<br /> </span><span style="color: #008080;"> 6</span> <span style="color: #000000;">  thread_specific_ptr(</span><span style="color: #0000ff;">void</span><span style="color: #000000;"> (</span><span style="color: #000000;">*</span><span style="color: #000000;">cleanup)(</span><span style="color: #0000ff;">void</span><span style="color: #000000;">*</span><span style="color: #000000;">));<br /> </span><span style="color: #008080;"> 7</span> <span style="color: #000000;">  </span><span style="color: #000000;">~</span><span style="color: #000000;">thread_specific_ptr();<br /> </span><span style="color: #008080;"> 8</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;"> 9</span> <span style="color: #000000;">  </span><span style="color: #008000;">//</span><span style="color: #008000;"> modifier functions</span><span style="color: #008000;"><br /> </span><span style="color: #008080;">10</span> <span style="color: #000000;">  T</span><span style="color: #000000;">*</span><span style="color: #000000;"> release();<br /> </span><span style="color: #008080;">11</span> <span style="color: #000000;">  </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> reset(T</span><span style="color: #000000;">*</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">);<br /> </span><span style="color: #008080;">12</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;">13</span> <span style="color: #000000;">  </span><span style="color: #008000;">//</span><span style="color: #008000;"> observer functions</span><span style="color: #008000;"><br /> </span><span style="color: #008080;">14</span> <span style="color: #000000;">  T</span><span style="color: #000000;">*</span><span style="color: #000000;"> get() </span><span style="color: #0000ff;">const</span><span style="color: #000000;">;<br /> </span><span style="color: #008080;">15</span> <span style="color: #000000;">  T</span><span style="color: #000000;">*</span><span style="color: #000000;"> operator</span><span style="color: #000000;">-></span><span style="color: #000000;">() </span><span style="color: #0000ff;">const</span><span style="color: #000000;">;<br /> </span><span style="color: #008080;">16</span> <span style="color: #000000;">  T</span><span style="color: #000000;">&</span><span style="color: #000000;"> operator</span><span style="color: #000000;">*</span><span style="color: #000000;">()() </span><span style="color: #0000ff;">const</span><span style="color: #000000;">;<br /> </span><span style="color: #008080;">17</span> <span style="color: #000000;">};</span></div> <br /> 鍗沖彲鏀寔get銆乺eset銆乺elease絳夋搷浣溿?br /> thread_specific_ptr綾葷殑瀹炵幇鍗佸垎綆鍗曪紝浠呬粎涓轟簡(jiǎn)灝唗ss綾?#8220;鏀硅”鎴愭櫤 鑳芥寚閽堢殑鏍峰瓙錛岃綾誨湪鍏舵瀯閫犲嚱鏁頒腑浼?xì)鑷姩鍒涘晦Z竴涓猼ss瀵硅薄錛岃屽湪鍏舵瀽鏋勫嚱鏁頒腑浼?xì)璋冪敤榛樿鍙傛暟鐨剅eset鍑芥暟錛屼粠鑰屽紩璧峰唴閮ㄨ灝佽鐨則ss瀵硅薄琚瀽鏋勶紝 杈懼埌“鑷姩”綆$悊鍐呭瓨鍒嗛厤閲婃斁鐨勭洰鐨勩?br /> <br /> 浠ヤ笅鏄竴涓繍鐢╰hread_specific_ptr瀹炵幇TSS鐨勪緥瀛愶細(xì)<font color="#000099"><br /> </font> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #008080;"> 1</span> <span style="color: #000000;">#include </span><span style="color: #000000;"><</span><span style="color: #000000;">boost</span><span style="color: #000000;">/</span><span style="color: #000000;">thread</span><span style="color: #000000;">/</span><span style="color: #000000;">thread.hpp</span><span style="color: #000000;">></span><span style="color: #000000;"><br /> </span><span style="color: #008080;"> 2</span> <span style="color: #000000;">#include </span><span style="color: #000000;"><</span><span style="color: #000000;">boost</span><span style="color: #000000;">/</span><span style="color: #000000;">thread</span><span style="color: #000000;">/</span><span style="color: #000000;">mutex.hpp</span><span style="color: #000000;">></span><span style="color: #000000;"><br /> </span><span style="color: #008080;"> 3</span> <span style="color: #000000;">#include </span><span style="color: #000000;"><</span><span style="color: #000000;">boost</span><span style="color: #000000;">/</span><span style="color: #000000;">thread</span><span style="color: #000000;">/</span><span style="color: #000000;">tss.hpp</span><span style="color: #000000;">></span><span style="color: #000000;"><br /> </span><span style="color: #008080;"> 4</span> <span style="color: #000000;">#include </span><span style="color: #000000;"><</span><span style="color: #000000;">iostream</span><span style="color: #000000;">></span><span style="color: #000000;"><br /> </span><span style="color: #008080;"> 5</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;"> 6</span> <span style="color: #000000;">boost::mutex io_mutex;<br /> </span><span style="color: #008080;"> 7</span> <span style="color: #000000;">boost::thread_specific_ptr</span><span style="color: #000000;"><</span><span style="color: #0000ff;">int</span><span style="color: #000000;">></span><span style="color: #000000;"> ptr;    </span><span style="color: #008000;">//</span><span style="color: #008000;"> use this method to tell that this member will not shared by all threads</span><span style="color: #008000;"><br /> </span><span style="color: #008080;"> 8</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;"> 9</span> <span style="color: #000000;">struct count<br /> </span><span style="color: #008080;">10</span> <span style="color: #000000;">{<br /> </span><span style="color: #008080;">11</span> <span style="color: #000000;">    count(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> id) : id(id) { }<br /> </span><span style="color: #008080;">12</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;">13</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> operator()()<br /> </span><span style="color: #008080;">14</span> <span style="color: #000000;">    {<br /> </span><span style="color: #008080;">15</span> <span style="color: #000000;">        </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (ptr.get() </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">)    </span><span style="color: #008000;">//</span><span style="color: #008000;"> if ptr is not initialized, initialize it</span><span style="color: #008000;"><br /> </span><span style="color: #008080;">16</span> <span style="color: #000000;">            ptr.reset(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;">(</span><span style="color: #000000;">0</span><span style="color: #000000;">));    </span><span style="color: #008000;">//</span><span style="color: #008000;"> Attention, we pass a pointer to reset (actually set ptr)</span><span style="color: #008000;"><br /> </span><span style="color: #008080;">17</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;">18</span> <span style="color: #000000;">        </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">; i </span><span style="color: #000000;"><</span><span style="color: #000000;"> </span><span style="color: #000000;">10</span><span style="color: #000000;">; </span><span style="color: #000000;">++</span><span style="color: #000000;">i)<br /> </span><span style="color: #008080;">19</span> <span style="color: #000000;">        {<br /> </span><span style="color: #008080;">20</span> <span style="color: #000000;">            (</span><span style="color: #000000;">*</span><span style="color: #000000;">ptr)</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br /> </span><span style="color: #008080;">21</span> <span style="color: #000000;">            boost::mutex::scoped_lock lock(io_mutex);<br /> </span><span style="color: #008080;">22</span> <span style="color: #000000;">            std::cout </span><span style="color: #000000;"><<</span><span style="color: #000000;"> id </span><span style="color: #000000;"><<</span><span style="color: #000000;"> </span><span style="color: #000000;">"</span><span style="color: #000000;">: </span><span style="color: #000000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;"><<</span><span style="color: #000000;"> </span><span style="color: #000000;">*</span><span style="color: #000000;">ptr </span><span style="color: #000000;"><<</span><span style="color: #000000;"> std::endl;<br /> </span><span style="color: #008080;">23</span> <span style="color: #000000;">        }<br /> </span><span style="color: #008080;">24</span> <span style="color: #000000;">    }<br /> </span><span style="color: #008080;">25</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;">26</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> id;<br /> </span><span style="color: #008080;">27</span> <span style="color: #000000;">};<br /> </span><span style="color: #008080;">28</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;">29</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> main(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> argc, </span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;"> argv[])<br /> </span><span style="color: #008080;">30</span> <span style="color: #000000;">{<br /> </span><span style="color: #008080;">31</span> <span style="color: #000000;">    boost::thread thrd1(count(</span><span style="color: #000000;">1</span><span style="color: #000000;">));<br /> </span><span style="color: #008080;">32</span> <span style="color: #000000;">    boost::thread thrd2(count(</span><span style="color: #000000;">2</span><span style="color: #000000;">));<br /> </span><span style="color: #008080;">33</span> <span style="color: #000000;">    thrd1.join();<br /> </span><span style="color: #008080;">34</span> <span style="color: #000000;">    thrd2.join();<br /> </span><span style="color: #008080;">35</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;">36</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">;<br /> </span><span style="color: #008080;">37</span> <span style="color: #000000;">}<br /> </span></div> 姝ゅ錛宼hread搴撹繕鎻愪緵浜?jiǎn)涓涓緢鏈夎叮鐨勫嚱鏁幫紝call_once錛屽湪tss<strong><font color="#663300">::</font></strong>init鐨勫疄鐜頒腑灝辯敤鍒頒簡(jiǎn)璇ュ嚱鏁般?br /> 璇ュ嚱鏁扮殑澹版槑濡備笅錛?font color="#ff6633"><br /> void</font> call_once<strong><font color="#663300">(</font></strong><font color="#ff6633">void</font><strong><font color="#663300"> (*</font></strong>func<strong><font color="#663300">)(),</font></strong> once_flag<strong><font color="#663300">&</font></strong> flag<strong><font color="#663300">);</font></strong><br /> 璇ュ嚱鏁扮殑Windows瀹炵幇閫氳繃鍒涘緩涓涓狹utex浣挎墍鏈夌殑綰跨▼鍦ㄥ皾璇曟墽琛岃鍑芥暟鏃跺浜庣瓑寰呯姸鎬侊紝鐩村埌鏈変竴涓嚎紼嬫墽琛屽畬浜?jiǎn)func鍑芥暟錛岃鍑芥暟鐨勭浜屼釜鍙傛暟琛ㄧず鍑芥暟func鏄惁宸茶鎵ц錛岃鍙傛暟寰寰琚垵濮嬪寲鎴怋OOST_ONCE_INIT錛堝嵆<font color="#999900">0</font>錛夛紝濡傛灉浣犲皢璇ュ弬鏁板垵濮嬪寲鎴?font color="#999900">1</font>錛屽垯鍑芥暟func灝嗕笉琚皟鐢紝姝ゆ椂call_once鐩稿綋浜庝粈涔堜篃娌″共錛岃繖鍦ㄦ湁鏃跺欏彲鑳芥槸闇瑕佺殑錛屾瘮濡傦紝鏍規(guī)嵁紼嬪簭澶勭悊鐨勭粨鏋滃喅瀹氭槸鍚﹂渶瑕乧all_once鏌愬嚱鏁癴unc銆?br /> call_once鍦ㄦ墽琛屽畬鍑芥暟func鍚庯紝浼?xì)灏唂lag淇敼涓?font color="#999900">1</font>錛岃繖鏍蜂細(xì)瀵艱嚧浠ュ悗鎵цcall_once鐨勭嚎紼嬶紙鍖呮嫭絳夊緟鍦∕utex澶勭殑綰跨▼鍜屽垰鍒氳繘鍏all_once鐨勭嚎紼嬶級(jí)閮戒細(xì)璺寵繃鎵цfunc鐨勪唬鐮併?br /> <br /> 闇瑕佹敞鎰忕殑鏄紝璇ュ嚱鏁頒笉鏄竴涓ā鏉垮嚱鏁幫紝鑰屾槸涓涓櫘閫氬嚱鏁幫紝瀹冪殑絎竴涓弬鏁?font color="#999900">1</font>鏄竴涓嚱鏁版寚閽堬紝鍏剁被鍨嬩負(fù)<font color="#ff6633">void</font><strong><font color="#663300"> (*)()</font></strong>錛岃屼笉鏄窡boost搴撶殑寰堝鍏跺畠鍦版柟涓鏍風(fēng)敤鐨勬槸function妯℃澘錛屼笉榪囪繖鏍蜂篃娌℃湁鍏崇郴錛屾湁浜?jiǎn)boost<strong><font color="#663300">::</font></strong>bind榪欎釜瓚呯駭姝﹀櫒錛屾兂鎬庝箞緇戝畾鍙傛暟灝遍殢浣犵殑渚夸簡(jiǎn)錛屾牴鎹産oost鐨勬枃妗o紝瑕佹眰浼犲叆鐨勫嚱鏁頒笉鑳芥姏鍑哄紓甯革紝浣嗕粠瀹炵幇浠g爜涓ソ鍍忎笉鏄繖鏍楓?br /> <br /> 浠ヤ笅鏄竴涓吀鍨嬬殑榪愮敤call_once瀹炵幇涓嬈″垵濮嬪寲鐨勪緥瀛愶細(xì)<font color="#000099"><br /> <br /> </font> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #008080;"> 1</span> <span style="color: #000000;">#include </span><span style="color: #000000;"><</span><span style="color: #000000;">boost</span><span style="color: #000000;">/</span><span style="color: #000000;">thread</span><span style="color: #000000;">/</span><span style="color: #000000;">thread.hpp</span><span style="color: #000000;">></span><span style="color: #000000;"><br /> </span><span style="color: #008080;"> 2</span> <span style="color: #000000;">#include </span><span style="color: #000000;"><</span><span style="color: #000000;">boost</span><span style="color: #000000;">/</span><span style="color: #000000;">thread</span><span style="color: #000000;">/</span><span style="color: #000000;">once.hpp</span><span style="color: #000000;">></span><span style="color: #000000;"><br /> </span><span style="color: #008080;"> 3</span> <span style="color: #000000;">#include </span><span style="color: #000000;"><</span><span style="color: #000000;">iostream</span><span style="color: #000000;">></span><span style="color: #000000;"><br /> </span><span style="color: #008080;"> 4</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">;<br /> </span><span style="color: #008080;"> 6</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> j </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">;<br /> </span><span style="color: #008080;"> 7</span> <span style="color: #000000;">boost::once_flag flag </span><span style="color: #000000;">=</span><span style="color: #000000;"> BOOST_ONCE_INIT;<br /> </span><span style="color: #008080;"> 8</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;"> 9</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> init()<br /> </span><span style="color: #008080;">10</span> <span style="color: #000000;">{<br /> </span><span style="color: #008080;">11</span> <span style="color: #000000;">    </span><span style="color: #000000;">++</span><span style="color: #000000;">i;<br /> </span><span style="color: #008080;">12</span> <span style="color: #000000;">}<br /> </span><span style="color: #008080;">13</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;">14</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> thread()<br /> </span><span style="color: #008080;">15</span> <span style="color: #000000;">{<br /> </span><span style="color: #008080;">16</span> <span style="color: #000000;">    boost::call_once(</span><span style="color: #000000;">&</span><span style="color: #000000;">init, flag);<br /> </span><span style="color: #008080;">17</span> <span style="color: #000000;">    </span><span style="color: #000000;">++</span><span style="color: #000000;">j;<br /> </span><span style="color: #008080;">18</span> <span style="color: #000000;">}<br /> </span><span style="color: #008080;">19</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;">20</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> main(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> argc, </span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;"> argv[])<br /> </span><span style="color: #008080;">21</span> <span style="color: #000000;">{<br /> </span><span style="color: #008080;">22</span> <span style="color: #000000;">    boost::thread thrd1(</span><span style="color: #000000;">&</span><span style="color: #000000;">thread);<br /> </span><span style="color: #008080;">23</span> <span style="color: #000000;">    boost::thread thrd2(</span><span style="color: #000000;">&</span><span style="color: #000000;">thread);<br /> </span><span style="color: #008080;">24</span> <span style="color: #000000;">    thrd1.join();<br /> </span><span style="color: #008080;">25</span> <span style="color: #000000;">    thrd2.join();<br /> </span><span style="color: #008080;">26</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;">27</span> <span style="color: #000000;">    std::cout </span><span style="color: #000000;"><<</span><span style="color: #000000;"> i </span><span style="color: #000000;"><<</span><span style="color: #000000;"> std::endl;<br /> </span><span style="color: #008080;">28</span> <span style="color: #000000;">    std::cout </span><span style="color: #000000;"><<</span><span style="color: #000000;"> j </span><span style="color: #000000;"><<</span><span style="color: #000000;"> std::endl;<br /> </span><span style="color: #008080;">29</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;">30</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">;<br /> </span><span style="color: #008080;">31</span> <span style="color: #000000;">}</span></div> 緇撴灉鏄劇ず錛屽叏灞鍙橀噺i浠呰鎵ц浜?jiǎn)涓嬈?strong><font color="#663300">++</font></strong>鎿嶄綔錛岃屽彉閲廽鍒欏湪涓や釜綰跨▼涓潎鎵ц浜?strong><font color="#663300">++</font></strong>鎿嶄綔銆?br /> <font color="#000099"><br /> </font> <img src ="http://m.tkk7.com/LittleDS/aggbug/201280.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/LittleDS/" target="_blank">鏉ㄧ</a> 2008-05-18 18:32 <a href="http://m.tkk7.com/LittleDS/archive/2008/05/18/201280.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Boost Thread瀛︿範(fàn)絎旇鍥?/title><link>http://m.tkk7.com/LittleDS/archive/2008/05/18/201262.html</link><dc:creator>鏉ㄧ</dc:creator><author>鏉ㄧ</author><pubDate>Sun, 18 May 2008 08:48:00 GMT</pubDate><guid>http://m.tkk7.com/LittleDS/archive/2008/05/18/201262.html</guid><wfw:comment>http://m.tkk7.com/LittleDS/comments/201262.html</wfw:comment><comments>http://m.tkk7.com/LittleDS/archive/2008/05/18/201262.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/LittleDS/comments/commentRss/201262.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/LittleDS/services/trackbacks/201262.html</trackback:ping><description><![CDATA[barrier<br /> barrier綾葷殑鎺ュ彛瀹氫箟濡備笅錛?br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> barrier : </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> boost::noncopyable   </span><span style="color: #008000;">//</span><span style="color: #008000;"> Exposition only</span><span style="color: #008000;"><br /> </span><span style="color: #008080;"> 2</span> <span style="color: #000000;">{<br /> </span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">public</span><span style="color: #000000;">:<br /> </span><span style="color: #008080;"> 4</span> <span style="color: #000000;">  </span><span style="color: #008000;">//</span><span style="color: #008000;"> construct/copy/destruct</span><span style="color: #008000;"><br /> </span><span style="color: #008080;"> 5</span> <span style="color: #000000;">  barrier(size_t n);<br /> </span><span style="color: #008080;"> 6</span> <span style="color: #000000;">  </span><span style="color: #000000;">~</span><span style="color: #000000;">barrier();<br /> </span><span style="color: #008080;"> 7</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;"> 8</span> <span style="color: #000000;">  </span><span style="color: #008000;">//</span><span style="color: #008000;"> waiting</span><span style="color: #008000;"><br /> </span><span style="color: #008080;"> 9</span> <span style="color: #000000;">  bool wait();<br /> </span><span style="color: #008080;">10</span> <span style="color: #000000;">};</span></div> <br /> barrier綾諱負(fù)鎴戜滑鎻愪緵浜?jiǎn)杩欐牱涓縐嶆帶鍒剁嚎紼嬪悓姝ョ殑鏈哄埗錛?br /> 鍓峮<strong><font color="#663300"> -</font></strong><font color="#999900"> 1</font>嬈¤皟鐢╳ait鍑芥暟灝嗚闃誨錛岀洿鍒扮n嬈¤皟鐢╳ait鍑芥暟錛岃屾鍚庣n<strong><font color="#663300"> +</font></strong><font color="#999900"> 1</font>嬈″埌絎?n<strong><font color="#663300"> -</font></strong><font color="#999900"> 1</font>嬈¤皟鐢╳ait涔熶細(xì)琚樆濉烇紝鐩村埌絎?n嬈¤皟鐢紝渚濇綾繪帹銆?br /> barrier<strong><font color="#663300">::</font></strong>wait鐨勫疄鐜板崄鍒嗙畝鍗曪細(xì)<br /> <br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #008080;"> 1</span> <span style="color: #000000;">barrier::barrier(unsigned </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> count)<br /> </span><span style="color: #008080;"> 2</span> <span style="color: #000000;">    : m_threshold(count), m_count(count), m_generation(</span><span style="color: #000000;">0</span><span style="color: #000000;">)<br /> </span><span style="color: #008080;"> 3</span> <span style="color: #000000;">{<br /> </span><span style="color: #008080;"> 4</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (count </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">)<br /> </span><span style="color: #008080;"> 5</span> <span style="color: #000000;">        </span><span style="color: #0000ff;">throw</span><span style="color: #000000;"> std::invalid_argument(</span><span style="color: #000000;">"</span><span style="color: #000000;">count cannot be zero.</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br /> </span><span style="color: #008080;"> 6</span> <span style="color: #000000;">}<br /> </span><span style="color: #008080;"> 7</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;"> 8</span> <span style="color: #000000;">bool barrier::wait()<br /> </span><span style="color: #008080;"> 9</span> <span style="color: #000000;">{<br /> </span><span style="color: #008080;">10</span> <span style="color: #000000;">    boost::mutex::scoped_lock lock(m_mutex);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> m_mutex is the base of barrier and is initilized by it's default constructor.</span><span style="color: #008000;"><br /> </span><span style="color: #008080;">11</span> <span style="color: #000000;">    unsigned </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> gen </span><span style="color: #000000;">=</span><span style="color: #000000;"> m_generation;    </span><span style="color: #008000;">//</span><span style="color: #008000;"> m_generation will be 0 for call 1~n-1, and 1 for n~2n - 1, and so on<img src="http://m.tkk7.com/Images/dot.gif" alt="" /></span><span style="color: #008000;"><br /> </span><span style="color: #008080;">12</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;">13</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #000000;">--</span><span style="color: #000000;">m_count </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">)<br /> </span><span style="color: #008080;">14</span> <span style="color: #000000;">    {<br /> </span><span style="color: #008080;">15</span> <span style="color: #000000;">        m_generation</span><span style="color: #000000;">++</span><span style="color: #000000;">;    </span><span style="color: #008000;">//</span><span style="color: #008000;"> cause m_generation to be changed in call n/2n/<img src="http://m.tkk7.com/Images/dot.gif" alt="" /></span><span style="color: #008000;"><br /> </span><span style="color: #008080;">16</span> <span style="color: #000000;">        m_count </span><span style="color: #000000;">=</span><span style="color: #000000;"> m_threshold;    </span><span style="color: #008000;">//</span><span style="color: #008000;"> reset count</span><span style="color: #008000;"><br /> </span><span style="color: #008080;">17</span> <span style="color: #000000;">        m_cond.notify_all();    </span><span style="color: #008000;">//</span><span style="color: #008000;"> wake up all thread waiting here</span><span style="color: #008000;"><br /> </span><span style="color: #008080;">18</span> <span style="color: #000000;">        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br /> </span><span style="color: #008080;">19</span> <span style="color: #000000;">    }<br /> </span><span style="color: #008080;">20</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;">21</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> (gen </span><span style="color: #000000;">==</span><span style="color: #000000;"> m_generation)    </span><span style="color: #008000;">//</span><span style="color: #008000;"> if m_generation is not changed, lock current thread.</span><span style="color: #008000;"><br /> </span><span style="color: #008080;">22</span> <span style="color: #000000;">        m_cond.wait(lock);<br /> </span><span style="color: #008080;">23</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br /> </span><span style="color: #008080;">24</span> <span style="color: #000000;">}</span></div> <br /> 鍥犳錛岃鐧戒簡(jiǎn)涔熶笉榪囨槸mutex鐨勪竴涓畝鍗曞簲鐢ㄣ?br /> 浠ヤ笅鏄竴涓嬌鐢╞arrier鐨勪緥瀛愶細(xì)<font color="#000099"><br /> <br /> </font> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #008080;"> 1</span> <span style="color: #000000;">#include </span><span style="color: #000000;"><</span><span style="color: #000000;">boost</span><span style="color: #000000;">/</span><span style="color: #000000;">thread</span><span style="color: #000000;">/</span><span style="color: #000000;">thread.hpp</span><span style="color: #000000;">></span><span style="color: #000000;"><br /> </span><span style="color: #008080;"> 2</span> <span style="color: #000000;">#include </span><span style="color: #000000;"><</span><span style="color: #000000;">boost</span><span style="color: #000000;">/</span><span style="color: #000000;">thread</span><span style="color: #000000;">/</span><span style="color: #000000;">barrier.hpp</span><span style="color: #000000;">></span><span style="color: #000000;"><br /> </span><span style="color: #008080;"> 3</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">;<br /> </span><span style="color: #008080;"> 5</span> <span style="color: #000000;">boost::barrier barr(</span><span style="color: #000000;">3</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> call barr.wait 3 * n times will release all threads in waiting</span><span style="color: #008000;"><br /> </span><span style="color: #008080;"> 6</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;"> 7</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> thread()<br /> </span><span style="color: #008080;"> 8</span> <span style="color: #000000;">{<br /> </span><span style="color: #008080;"> 9</span> <span style="color: #000000;">    </span><span style="color: #000000;">++</span><span style="color: #000000;">i;<br /> </span><span style="color: #008080;">10</span> <span style="color: #000000;">    barr.wait();<br /> </span><span style="color: #008080;">11</span> <span style="color: #000000;">}<br /> </span><span style="color: #008080;">12</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;">13</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> main()<br /> </span><span style="color: #008080;">14</span> <span style="color: #000000;">{<br /> </span><span style="color: #008080;">15</span> <span style="color: #000000;">    boost::thread thrd1(</span><span style="color: #000000;">&</span><span style="color: #000000;">thread);<br /> </span><span style="color: #008080;">16</span> <span style="color: #000000;">    boost::thread thrd2(</span><span style="color: #000000;">&</span><span style="color: #000000;">thread);<br /> </span><span style="color: #008080;">17</span> <span style="color: #000000;">    boost::thread thrd3(</span><span style="color: #000000;">&</span><span style="color: #000000;">thread);<br /> </span><span style="color: #008080;">18</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;">19</span> <span style="color: #000000;">    thrd1.join();<br /> </span><span style="color: #008080;">20</span> <span style="color: #000000;">    thrd2.join();<br /> </span><span style="color: #008080;">21</span> <span style="color: #000000;">    thrd3.join();<br /> </span><span style="color: #008080;">22</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;">23</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">;<br /> </span><span style="color: #008080;">24</span> <span style="color: #000000;">}</span></div> <br /> 濡傛灉鍘繪帀鍏朵腑thrd3鐩稿叧鐨勪唬鐮侊紝灝嗕嬌寰楃嚎紼?font color="#999900">1</font>銆?font color="#999900">2</font>涓鐩村浜巜ait鐘舵侊紝榪涜屼嬌寰椾富綰跨▼鏃犳硶閫鍑恒?br /> <br /> xtime<br /> xtime鏄痓oost<strong><font color="#663300">::</font></strong>thread涓敤鏉ヨ〃紺烘椂闂寸殑涓涓緟鍔╃被錛屽畠鏄竴涓粎鍖呭惈涓や釜鎴愬憳鍙橀噺鐨勭粨鏋勪綋錛?font color="#990000"><br /> <br /> </font> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #008080;">1</span> <span style="color: #000000;">struct xtime<br /> </span><span style="color: #008080;">2</span> <span style="color: #000000;">{<br /> </span><span style="color: #008080;">3</span> <span style="color: #008000;">//</span><span style="color: #008000;"><img src="http://m.tkk7.com/Images/dot.gif" alt="" /></span><span style="color: #008000;"><br /> </span><span style="color: #008080;">4</span> <span style="color: #000000;">    xtime_sec_t sec;<br /> </span><span style="color: #008080;">5</span> <span style="color: #000000;">    xtime_nsec_t nsec;<br /> </span><span style="color: #008080;">6</span> <span style="color: #000000;">};</span></div> <br /> condition<strong><font color="#663300">::</font></strong>timed_wait銆乼hread<strong><font color="#663300">::</font></strong>sleep絳夋秹鍙?qiáng)瓒呮椂鐨勫嚱鏁伴渶瑕佺敤鍒皒time銆?br /> 闇瑕佹敞鎰忕殑鏄紝xtime琛ㄧず鐨勪笉鏄竴涓椂闂撮棿闅旓紝鑰屾槸涓涓椂闂寸偣錛屽洜姝や嬌鐢ㄨ搗鏉ュ緢涓嶆柟渚褲備負(fù)浜?jiǎn)鏂逛究鋴社敤xtime錛宐oost鎻愪緵浜?jiǎn)涓浜涜緟鍔╃殑xtime鎿嶄綔鍑芥暟錛屽xtime_get銆亁time_cmp絳夈?br /> 浠ヤ笅鏄竴涓嬌鐢▁time鏉ユ墽琛宻leep鐨勪緥瀛愶紙璺熺畝鍗曠殑涓鍙leep姣旇搗鏉ワ紝瀹炲湪鏄お澶嶆潅浜?jiǎn)锛?jí)錛屽叾涓敤鍒頒簡(jiǎn)xtime鍒濆鍖栧嚱鏁皒time_get錛?br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #008080;"> 1</span> <span style="color: #000000;">#include </span><span style="color: #000000;"><</span><span style="color: #000000;">boost</span><span style="color: #000000;">/</span><span style="color: #000000;">thread</span><span style="color: #000000;">/</span><span style="color: #000000;">thread.hpp</span><span style="color: #000000;">></span><span style="color: #000000;"><br /> </span><span style="color: #008080;"> 2</span> <span style="color: #000000;">#include </span><span style="color: #000000;"><</span><span style="color: #000000;">boost</span><span style="color: #000000;">/</span><span style="color: #000000;">thread</span><span style="color: #000000;">/</span><span style="color: #000000;">xtime.hpp</span><span style="color: #000000;">></span><span style="color: #000000;"><br /> </span><span style="color: #008080;"> 3</span> <span style="color: #000000;">#include </span><span style="color: #000000;"><</span><span style="color: #000000;">iostream</span><span style="color: #000000;">></span><span style="color: #000000;"><br /> </span><span style="color: #008080;"> 4</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> main()<br /> </span><span style="color: #008080;"> 6</span> <span style="color: #000000;">{<br /> </span><span style="color: #008080;"> 7</span> <span style="color: #000000;">    boost::xtime xt;<br /> </span><span style="color: #008080;"> 8</span> <span style="color: #000000;">    boost::xtime_get(</span><span style="color: #000000;">&</span><span style="color: #000000;">xt, boost::TIME_UTC);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> initialize xt with current time</span><span style="color: #008000;"><br /> </span><span style="color: #008080;"> 9</span> <span style="color: #000000;">    xt.sec </span><span style="color: #000000;">+=</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">;    </span><span style="color: #008000;">//</span><span style="color: #008000;"> change xt to next second</span><span style="color: #008000;"><br /> </span><span style="color: #008080;">10</span> <span style="color: #000000;">    boost::thread::sleep(xt);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> do sleep</span><span style="color: #008000;"><br /> </span><span style="color: #008080;">11</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;">12</span> <span style="color: #000000;">    std::cout </span><span style="color: #000000;"><<</span><span style="color: #000000;"> </span><span style="color: #000000;">"</span><span style="color: #000000;">1 second sleep over.</span><span style="color: #000000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;"><<</span><span style="color: #000000;"> std::endl;<br /> </span><span style="color: #008080;">13</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;">14</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">;<br /> </span><span style="color: #008080;">15</span> <span style="color: #000000;">} <br /> </span></div> <br /> <br /> <br /> <font color="#000099"><br /> </font> <img src ="http://m.tkk7.com/LittleDS/aggbug/201262.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/LittleDS/" target="_blank">鏉ㄧ</a> 2008-05-18 16:48 <a href="http://m.tkk7.com/LittleDS/archive/2008/05/18/201262.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Boost Thread瀛︿範(fàn)絎旇涓?/title><link>http://m.tkk7.com/LittleDS/archive/2008/05/18/201261.html</link><dc:creator>鏉ㄧ</dc:creator><author>鏉ㄧ</author><pubDate>Sun, 18 May 2008 08:45:00 GMT</pubDate><guid>http://m.tkk7.com/LittleDS/archive/2008/05/18/201261.html</guid><wfw:comment>http://m.tkk7.com/LittleDS/comments/201261.html</wfw:comment><comments>http://m.tkk7.com/LittleDS/archive/2008/05/18/201261.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/LittleDS/comments/commentRss/201261.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/LittleDS/services/trackbacks/201261.html</trackback:ping><description><![CDATA[     鎽樿: 涓嬮潰鍏堝condition_impl榪涜綆瑕佸垎鏋愩?condition_impl鍦ㄥ叾鏋勯犲嚱鏁頒腑浼?xì)鍒涘晦Z袱涓猄emaphore錛堜俊鍙烽噺錛夛細(xì)m_gate銆乵_queue錛屽強(qiáng)涓涓狹utex錛堜簰鏂ヤ綋錛岃窡boost::mutex綾諱技錛屼絾boost::mutex鏄熀浜嶤riticalSection<涓寸晫鍖?gt;鐨勶級(jí)錛歮_mutex錛屽叾涓細(xì) m_queue 鐩稿綋浜庡綋鍓嶆墍鏈夌瓑寰呯嚎紼嬬殑絳夊緟闃熷垪錛屾瀯閫犲嚱鏁?..  <a href='http://m.tkk7.com/LittleDS/archive/2008/05/18/201261.html'>闃呰鍏ㄦ枃</a><img src ="http://m.tkk7.com/LittleDS/aggbug/201261.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/LittleDS/" target="_blank">鏉ㄧ</a> 2008-05-18 16:45 <a href="http://m.tkk7.com/LittleDS/archive/2008/05/18/201261.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Boost Thread瀛︿範(fàn)絎旇浜?/title><link>http://m.tkk7.com/LittleDS/archive/2008/05/18/201253.html</link><dc:creator>鏉ㄧ</dc:creator><author>鏉ㄧ</author><pubDate>Sun, 18 May 2008 08:20:00 GMT</pubDate><guid>http://m.tkk7.com/LittleDS/archive/2008/05/18/201253.html</guid><wfw:comment>http://m.tkk7.com/LittleDS/comments/201253.html</wfw:comment><comments>http://m.tkk7.com/LittleDS/archive/2008/05/18/201253.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/LittleDS/comments/commentRss/201253.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/LittleDS/services/trackbacks/201253.html</trackback:ping><description><![CDATA[闄や簡(jiǎn)thread錛宐oost<strong><font color="#663300">::</font></strong>thread鍙︿竴涓噸瑕佺粍鎴愰儴鍒嗘槸mutex錛屼互鍙?qiáng)宸ヤ綔鍦╩utex涓婄殑boost<strong><font color="#663300">::</font></strong>mutex<strong><font color="#663300">::</font></strong>scoped_lock銆乧ondition鍜宐arrier錛岃繖浜涢兘鏄負(fù)瀹炵幇綰跨▼鍚屾鎻愪緵鐨勩?br /> <br /> mutex<br /> boost鎻愪緵鐨刴utex鏈?font color="#999900">6</font>縐嶏細(xì)<br /> boost<strong><font color="#663300">::</font></strong>mutex<br /> boost<strong><font color="#663300">::</font></strong>try_mutex<br /> boost<strong><font color="#663300">::</font></strong>timed_mutex<br /> boost<strong><font color="#663300">::</font></strong>recursive_mutex<br /> boost<strong><font color="#663300">::</font></strong>recursive_try_mutex<br /> boost<strong><font color="#663300">::</font></strong>recursive_timed_mutex<br /> 涓嬮潰浠呭boost<strong><font color="#663300">::</font></strong>mutex榪涜鍒嗘瀽銆?br /> mutex綾繪槸涓涓狢riticalSection錛堜復(fù)鐣屽尯錛夊皝瑁呯被錛屽畠鍦ㄦ瀯閫犲嚱鏁頒腑鏂板緩涓涓復(fù)鐣屽尯騫禝nitializeCriticalSection錛岀劧鍚庣敤涓涓垚鍛樺彉閲?font color="#ff6633"><br /> void</font><strong><font color="#663300">*</font></strong> m_mutex<strong><font color="#663300">;</font></strong><br /> 鏉ヤ繚瀛樿涓寸晫鍖虹粨鏋勩?br /> 闄? 姝や箣澶栵紝mutex榪樻彁渚涗簡(jiǎn)do_lock銆乨o_unlock絳夋柟娉曪紝榪欎簺鏂規(guī)硶鍒嗗埆璋冪敤EnterCriticalSection銆? LeaveCriticalSection鏉ヤ慨鏀規(guī)垚鍛樺彉閲弇_mutex錛圕RITICAL_SECTION緇撴瀯鎸囬拡錛夌殑鐘舵侊紝浣嗚繖浜涙柟娉曢兘鏄?font color="#990000">private</font>鐨勶紝浠ラ槻姝㈡垜浠洿鎺ュmutex榪涜閿佹搷浣滐紝鎵鏈夌殑閿佹搷浣滈兘蹇呴』閫氳繃mutex鐨勫弸鍏冪被detail<strong><font color="#663300">::</font></strong>thread<strong><font color="#663300">::</font></strong>lock_ops<strong><font color="#663300"><</font></strong>mutex<strong><font color="#663300">></font></strong>鏉ュ畬鎴愶紝姣旇緝鏈夋剰鎬濈殑鏄紝lock_ops鐨勬墍鏈夋柟娉曪細(xì)lock銆乽nlock銆乼rylock絳夐兘鏄?font color="#990000">static</font>鐨勶紝濡俵ock_ops<strong><font color="#663300"><</font></strong>Mutex<strong><font color="#663300">>::</font></strong>lock鐨勫疄鐜幫細(xì)<br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 0);">template </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">typename Mutex</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> lock_ops : </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> noncopyable<br /> </span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);">{<br /> </span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);"><img src="http://m.tkk7.com/Images/dot.gif" alt="" /><br /> </span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">:<br /> </span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> lock(Mutex</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);"> m)<br /> </span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);">    {<br /> </span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">        m.do_lock();<br /> </span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">    }<br /> </span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);"><img src="http://m.tkk7.com/Images/dot.gif" alt="" /><br /> </span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">}</span></div> boost<strong><font color="#663300">::</font></strong>thread鐨勮璁¤呬負(fù)浠涔堜細(xì)榪欎箞璁捐鍛紵鎴戞兂澶ф鏄細(xì)<font color="#999900"><br /> 1</font>銆乥oost<strong><font color="#663300">::</font></strong>thread鐨勮璁¤呬笉甯屾湜琚垜浠洿鎺ユ搷浣渕utex錛屾敼鍙樺叾鐘舵侊紝鎵浠utex鐨勬墍鏈夋柟娉曢兘鏄?font color="#990000">private</font>鐨勶紙闄や簡(jiǎn)鏋勯犲嚱鏁幫紝鏋愭瀯鍑芥暟錛夈?font color="#999900"><br /> 2</font>銆佽櫧鐒舵垜浠彲浠ラ氳繃lock_ops鏉ヤ慨鏀筸utex鐨勭姸鎬侊紝濡傦細(xì)<br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">boost</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">thread</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">thread.hpp</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">boost</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">thread</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">mutex.hpp</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">boost</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">thread</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">detail</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">lock.hpp</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main()<br /> </span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);">{<br /> </span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);">    boost::mutex mt;<br /> </span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">mt.do_lock();        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> Error! Can not access private member!</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">    boost::detail::thread::lock_ops</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">boost::mutex</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);">::lock(mt);<br /> </span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /> </span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">}</span></div> 浣嗘槸錛岃繖鏄笉鎺ㄨ崘鐨勶紝鍥犱負(fù)mutex銆乻coped_lock銆乧ondition銆乥arrier鏄竴濂楀畬鏁寸殑綾葷郴錛屽畠浠槸鐩鎬簰鍗忓悓宸ヤ綔鐨勶紝鍍忎笂闈㈣繖涔堟搷浣滄病鏈夊姙娉曚笌鍚庨潰鐨勫嚑涓被鍗忓悓宸ヤ綔銆?br /> scoped_lock<br /> 涓婇潰璇磋繃錛屼笉搴旇鐩存帴鐢╨ock_ops鏉ユ搷浣渕utex瀵硅薄錛岄偅涔堬紝搴旇鐢ㄤ粈涔堝憿錛熺瓟妗堝氨鏄痵coped_lock銆備笌瀛樺湪澶氱mutex涓鏍鳳紝瀛樺湪澶氱涓巑utex瀵瑰簲鐨剆coped_lock錛?br /> <br /> scoped_lock<br /> scoped_try_lock<br /> scoped_timed_lock<br /> <br /> 榪欓噷鎴戜滑鍙璁簊coped_lock銆?br /> scoped_lock鏄畾涔夊湪<font color="#990000">namespace</font> boost<strong><font color="#663300">::</font></strong>detail<strong><font color="#663300">::</font></strong>thread涓嬬殑錛屼負(fù)浜?jiǎn)鏂逛究鎴戜滑鋴社敤锛堜篃湄?fù)浜?jiǎn)鏂逛究璁捐鑰咃級(jí)錛宮utex浣跨敤浜?jiǎn)涓嬮潰鐨?font color="#990000">typedef</font>錛?font color="#990000"><br /> typedef</font> detail<strong><font color="#663300">::</font></strong>thread<strong><font color="#663300">::</font></strong>scoped_lock<strong><font color="#663300"><</font></strong>mutex<strong><font color="#663300">></font></strong> scoped_lock<strong><font color="#663300">;</font></strong><br /> 榪欐牱鎴戜滑灝卞彲浠ラ氳繃錛?br /> boost<strong><font color="#663300">::</font></strong>mutex<strong><font color="#663300">::</font></strong>scoped_lock<br /> 鏉ヤ嬌鐢╯coped_lock綾繪ā鏉夸簡(jiǎn)銆?br /> 鐢變簬scoped_lock鐨勪綔鐢ㄤ粎鍦ㄤ簬瀵筸utex鍔犻攣<strong><font color="#663300">/</font></strong>瑙i攣錛堝嵆浣縨utex EnterCriticalSection<strong><font color="#663300">/</font></strong>LeaveCriticalSection錛夛紝鍥犳錛屽畠鐨勬帴鍙d篃寰堢畝鍗曪紝闄や簡(jiǎn)鏋勯犲嚱鏁板錛屼粎鏈塴ock<strong><font color="#663300">/</font></strong>unlock<strong><font color="#663300">/</font></strong>locked錛堝垽鏂槸鍚﹀凡鍔犻攣錛夛紝鍙?qiáng)绫诲瀷铦{鎹㈡搷浣滅<font color="#ff6633">void</font><strong><font color="#663300">*</font></strong>錛屼竴鑸垜浠笉闇瑕佹樉寮忚皟鐢ㄨ繖浜涙柟娉曪紝鍥犱負(fù)scoped_lock鐨勬瀯閫犲嚱鏁版槸榪欐牱瀹氫箟鐨勶細(xì)<br /> <br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 128, 128);">1</span> <span style="color: rgb(0, 0, 0);">explicit scoped_lock(Mutex</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);"> mx, bool initially_locked</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">)<br /> </span><span style="color: rgb(0, 128, 128);">2</span> <span style="color: rgb(0, 0, 0);">    : m_mutex(mx), m_locked(</span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">)<br /> </span><span style="color: rgb(0, 128, 128);">3</span> <span style="color: rgb(0, 0, 0);">{<br /> </span><span style="color: rgb(0, 128, 128);">4</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (initially_locked) lock();<br /> </span><span style="color: rgb(0, 128, 128);">5</span> <span style="color: rgb(0, 0, 0);">}</span></div> <br /> 娉細(xì)m_mutex鏄竴涓猰utex鐨勫紩鐢ㄣ?br /> 鍥犳錛屽綋鎴戜滑涓嶆寚瀹歩nitially_locked鍙傛暟鏋勯犱竴涓猻coped_lock瀵硅薄 鏃訛紝scoped_lock浼?xì)鑷姩瀵规墍緇戝畾鐨刴utex鍔犻攣錛岃屾瀽鏋勫嚱鏁頒細(xì)媯(gè)鏌ユ槸鍚﹀姞閿侊紝鑻ュ凡鍔犻攣錛屽垯瑙i攣錛涘綋鐒?dòng)灱屾湁浜涙儏鍐典笅锛屾垜浠彲鑳戒笉闇瑕佹瀯閫犳椂鑷姩 鍔犻攣錛岃繖鏍峰氨闇瑕佽嚜宸辮皟鐢╨ock鏂規(guī)硶銆傚悗闈㈢殑condition銆乥arrier涔熶細(xì)璋冪敤scoped_lock鐨刲ock銆乽nlock鏂規(guī)硶鏉ュ疄鐜伴儴 鍒嗘柟娉曘?br /> 姝e洜涓簊coped_lock鍏鋒湁鍙湪鏋勯犳椂鍔犻攣錛屾瀽鏋勬椂瑙i攣鐨勭壒鎬э紝鎴戜滑緇忓父浼?xì)鋴社敤灞閮ㄥ彉閲忔潵瀹炵幇瀵筸utex鐨勭嫭鍗犺闂?br /> <br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">boost</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">thread</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">thread.hpp</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">boost</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">thread</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">mutex.hpp</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);">boost::mutex io_mutex;<br /> </span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> count()    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> worker function</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">{<br /> </span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">; i </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">; </span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br /> </span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">    {<br /> </span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">        boost::mutex::scoped_lock lock(io_mutex);<br /> </span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">        std::cout </span><span style="color: rgb(0, 0, 0);"><<</span><span style="color: rgb(0, 0, 0);"> i </span><span style="color: rgb(0, 0, 0);"><<</span><span style="color: rgb(0, 0, 0);"> std::endl;<br /> </span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">    }<br /> </span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">}<br /> </span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc, </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> argv[])<br /> </span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">{<br /> </span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">    boost::thread thrd1(</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">count);<br /> </span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">    boost::thread thrd2(</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">count);<br /> </span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">    thrd1.join();<br /> </span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);">    thrd2.join();<br /> </span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /> </span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);">}</span></div> <br /> 鍦ㄦ瘡嬈¤緭鍑轟俊鎭椂錛屼負(fù)浜?jiǎn)闃叉鏁翠釜杈撳國(guó)櫩嚱E嬭鍏跺畠綰跨▼鎵撲貢錛岄氳繃瀵筰o_mutex鍔犻攣錛堣繘鍏ヤ復(fù)鐣屽尯錛夛紝浠庤屼繚璇佷簡(jiǎn)杈撳嚭鐨勬紜с?br /> 鍦ㄤ嬌鐢? scoped_lock鏃訛紝鎴戜滑鏈夋椂鍊欓渶瑕佷嬌鐢ㄥ叏灞閿侊紙瀹氫箟涓涓叏灞mutex錛屽綋闇瑕佺嫭鍗犺闂叏灞璧勬簮鏃訛紝浠ヨ鍏ㄥ眬mutex涓哄弬鏁版瀯閫犱竴涓? scoped_lock瀵硅薄鍗沖彲銆傚叏灞mutex鍙互鏄叏灞鍙橀噺錛屼篃鍙互鏄被鐨勯潤(rùn)鎬佹柟娉曠瓑錛夛紝鏈夋椂鍊欏垯闇瑕佷嬌鐢ㄥ璞¢攣錛堝皢mutex瀹氫箟鎴愮被鐨勬垚鍛樺彉 閲忥級(jí)錛屽簲璇ユ牴鎹渶瑕佽繘琛屽悎鐞嗛夋嫨銆?br /> Java鐨剆ynchronized鍙敤浜庡鏂規(guī)硶鍔犻攣錛屽浠g爜孌靛姞閿侊紝瀵瑰璞″姞閿侊紝瀵圭被鍔犻攣錛堜粛鐒舵槸瀵硅薄綰? 鐨勶級(jí)錛岃繖鍑犵鍔犻攣鏂瑰紡閮藉彲浠ラ氳繃涓婇潰璁茬殑瀵硅薄閿佹潵妯℃嫙錛涚浉鍙嶏紝鍦↗ava涓疄鐜板叏灞閿佸ソ鍍忔湁鐐歸夯鐑?chǔ)锛屽繀椤粸畣璇锋眰灏佽鍒熬c諱腑錛屼互杞崲鎴愪笂闈㈢殑鍥涚 synchronized褰㈠紡涔嬩竴銆?br /> <br /> condition<br /> condition鐨勬帴鍙e涓嬶細(xì)<font color="#990000"><br /> <br /> </font> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> condition : </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> boost::noncopyable   </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> Exposition only</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);">{<br /> </span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">:<br /> </span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> construct/copy/destruct</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);">  condition();<br /> </span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 0);">~</span><span style="color: rgb(0, 0, 0);">condition();<br /> </span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> notification</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> notify_one();<br /> </span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> notify_all();<br /> </span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> waiting</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">  template</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">typename ScopedLock</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> wait(ScopedLock</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">);<br /> </span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">  template</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">typename ScopedLock, typename Pred</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> wait(ScopedLock</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">, Pred);<br /> </span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">  template</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">typename ScopedLock</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">    bool timed_wait(ScopedLock</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 255);">const</span><span style="color: rgb(0, 0, 0);"> boost::xtime</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">);<br /> </span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">  template</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">typename ScopedLock, typename Pred</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">    bool timed_wait(ScopedLock</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">, Pred);<br /> </span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">};</span></div> <br /> 鍏朵腑wait鐢ㄤ簬絳夊緟鏌愪釜condition鐨勫彂鐢燂紝鑰宼imed_wait鍒欐彁渚涘叿鏈夎秴鏃剁殑wait鍔熻兘錛宯otify_one鐢ㄤ簬鍞ら啋涓涓瓑寰呰condition鍙戠敓鐨勭嚎紼嬶紝notify_all鍒欑敤浜庡敜閱掓墍鏈夌瓑寰呰condition鍙戠敓鐨勭嚎紼嬨?br /> <br /> 鐢變簬condition鐨勮涔夌浉瀵硅緝涓哄鏉傦紝瀹冪殑瀹炵幇涔熸槸鏁翠釜boost<strong><font color="#663300">::</font></strong>thread搴撲腑鏈澶嶆潅鐨勶紙瀵筗indows鐗堟湰鑰岃█錛屽鏀寔pthread鐨勭増鏈岃█錛岀敱浜巔thread宸茬粡鎻愪緵浜?jiǎn)pthread_cond_t錛屼嬌寰梒ondition瀹炵幇璧鋒潵涔熷崄鍒嗙畝鍗曪級(jí)錛屼笅闈㈠wait鍜宯otify_one榪涜綆瑕佸垎鏋愩?br /> condition鍐呴儴鍖呭惈浜?jiǎn)涓涓猚ondition_impl瀵硅薄錛岀敱璇ュ璞℃墽琛屾潵澶勭悊瀹為檯鐨剋ait銆乶otify_one<strong><font color="#663300">...</font></strong>絳夋搷浣溿?br /> <br /> <br /> <br /> <img src ="http://m.tkk7.com/LittleDS/aggbug/201253.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/LittleDS/" target="_blank">鏉ㄧ</a> 2008-05-18 16:20 <a href="http://m.tkk7.com/LittleDS/archive/2008/05/18/201253.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Boost Thread瀛︿範(fàn)絎旇http://m.tkk7.com/LittleDS/archive/2008/05/18/201236.html鏉ㄧ鏉ㄧSun, 18 May 2008 06:49:00 GMThttp://m.tkk7.com/LittleDS/archive/2008/05/18/201236.htmlhttp://m.tkk7.com/LittleDS/comments/201236.htmlhttp://m.tkk7.com/LittleDS/archive/2008/05/18/201236.html#Feedback0http://m.tkk7.com/LittleDS/comments/commentRss/201236.htmlhttp://m.tkk7.com/LittleDS/services/trackbacks/201236.html::thread搴撶殑涓? 瑙掞紝浣唗hread綾葷殑瀹炵幇鎬諱綋涓婃槸姣旇緝綆鍗曠殑錛屽墠闈㈠凡緇忚榪囷紝thread鍙槸涓涓法騫沖彴鐨勭嚎紼嬪皝瑁呭簱錛屽叾涓寜鐓ф墍浣跨敤鐨勭紪璇戦夐」鐨勪笉鍚岋紝鍒嗗埆鍐沖畾浣跨敤 Windows綰跨▼API榪樻槸pthread錛屾垨鑰匨acintosh Carbon騫沖彴鐨則hread瀹炵幇銆備互涓嬪彧璁ㄨWindows錛屽嵆浣跨敤 BOOST_HAS_WINTHREADS鐨勬儏鍐點(diǎn)?br /> thread綾繪彁渚涗簡(jiǎn)涓ょ鏋勯犲嚱鏁幫細(xì)
thread::thread()
thread::thread(const function0<void>& threadfunc)
絎? 涓縐嶆瀯閫犲嚱鏁扮敤浜庤皟鐢℅etCurrentThread鏋勯犱竴涓綋鍓嶇嚎紼嬬殑thread瀵硅薄錛岀浜岀鍒欓氳繃浼犲叆涓涓嚱鏁版垨鑰呬竴涓猣unctor鏉ュ垱寤轟竴涓? 鏂扮殑綰跨▼銆傜浜岀鎯呭喌涓嬶紝thread綾誨湪鍏舵瀯閫犲嚱鏁頒腑闂存帴璋冪敤CreateThread鏉ュ垱寤虹嚎紼嬶紝騫跺皢綰跨▼鍙ユ焺淇濆瓨鍒版垚鍛樺彉閲弇_thread涓紝騫? 鎵ц浼犲叆鐨勫嚱鏁幫紝鎴栨墽琛宖unctor鐨?font color="#990000">operator ()鏂規(guī)硶鏉ュ惎鍔ㄥ伐浣滅嚎紼嬨?br />
鎴戜滑鍙互鐢ㄤ互涓嬩笁縐嶆柟寮忓惎鍔ㄤ竴涓柊綰跨▼錛?font color="#999900">
1銆佷紶閫掍竴涓伐浣滃嚱鏁版潵鏋勯犱竴涓伐浣滅嚎紼?font color="#000099">

 1 #include <boost/thread/thread.hpp>
 2 #include <boost/thread/mutex.hpp>
 3 #include <iostream>
 4 
 5 boost::mutex io_mutex;
 6 
 7 void count()    // worker function
 8 {
 9     for (int i = 0; i < 10++i)
10     {
11         boost::mutex::scoped_lock lock(io_mutex);
12         std::cout << i << std::endl;
13     }
14 }
15 
16 int main(int argc, char* argv[])
17 {
18     boost::thread thrd1(&count);
19     boost::thread thrd2(&count);
20     thrd1.join();
21     thrd2.join();
22 
23     return 0;
24 }
25 

2銆佷紶閫掍竴涓猣unctor瀵硅薄鏉ユ瀯閫犱竴涓伐浣滅嚎紼?font color="#000099">

 1 #include <boost/thread/thread.hpp>
 2 #include <boost/thread/mutex.hpp>
 3 #include <iostream>
 4 
 5 boost::mutex io_mutex;
 6 
 7 struct count
 8 {
 9     count(int id) : id(id) { }
10 
11     void operator()()
12     {
13         for (int i = 0; i < 10++i)
14         {
15             boost::mutex::scoped_lock lock(io_mutex);        // lock io, will be explained soon.
16             std::cout << id << "" << i << std::endl;
17         }
18     }
19 
20     int id;
21 };
22 
23 int main(int argc, char* argv[])
24 {
25     boost::thread thrd1(count(1));
26     boost::thread thrd2(count(2));
27     thrd1.join();
28     thrd2.join();
29     return 0;
30 }
31 

3銆佹棤闇灝嗙被璁捐鎴愪竴涓猣unctor錛屽熷姪bind鏉ユ瀯閫爁unctor瀵硅薄浠ュ垱寤哄伐浣滅嚎紼?br />
 1 #include <boost/thread/thread.hpp>
 2 #include <boost/thread/mutex.hpp>
 3 #include <boost/bind.hpp>
 4 #include <iostream>
 5 
 6 boost::mutex io_mutex;
 7 
 8 struct count
 9 {
10     static int num;
11     int id;
12 
13     count() : id(num++) {}
14 
15     int do_count(int n)
16     {
17         for (int i = 0; i < n; ++i)
18         {
19             boost::mutex::scoped_lock lock(io_mutex);
20             std::cout << id << "" << i << std::endl;
21         }
22         return id;
23     }
24 };
25 
26 int count::num = 1;
27 
28 int main(int argc, char* argv[])
29 {
30     count c1;
31     boost::thread thrd1(boost::bind(&count::do_count, &c1, 10));
32     thrd1.join();
33     return 0;
34 }

鍏朵腑bind鏄竴涓嚱鏁版ā鏉匡紝瀹冨彲浠ユ牴鎹悗闈㈢殑瀹炰緥鍖栧弬鏁版瀯閫犲嚭涓涓猣unctor鏉ワ紝涓婇潰鐨刡oost::bind(&count::do_count, &c1, 10)鍏跺疄絳変環(huán)浜庤繑鍥炰簡(jiǎn)涓涓猣unctor錛?font color="#990000">
struct countFunctor
{

    int
 operator() ()
    {
        (&
c1)->do_count(10);    // just a hint, not actual code
    }
};

鍥犳錛屼互鍚庡氨璺?font color="#999900">2涓槸涓鏍風(fēng)殑浜?jiǎn)銆?br />



鏉ㄧ 2008-05-18 14:49 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 日韩亚洲国产二区| 免费又黄又爽又猛的毛片| 全免费a级毛片免费看无码| 国产男女性潮高清免费网站| 亚洲精品A在线观看| 亚洲国产精品VA在线观看麻豆 | 免费看大美女大黄大色| 亚洲国产日韩在线观频| 亚洲天天做日日做天天欢毛片| 亚洲五月综合缴情婷婷| 美女视频黄频a免费观看| 免费日本一区二区| 女人与禽交视频免费看| 77777亚洲午夜久久多人| 91情国产l精品国产亚洲区 | 亚洲精品无码不卡| 中文字幕在线日亚洲9| xxxxxx日本处大片免费看| 91免费人成网站在线观看18| 永久免费AV无码网站在线观看| 在线亚洲人成电影网站色www| 亚洲欧洲日产国码www| 污视频网站在线免费看| 最近2019中文字幕免费大全5| 最新69国产成人精品免费视频动漫| 亚洲午夜久久久久久久久电影网 | 中文字幕免费在线观看动作大片| 18禁成人网站免费观看| 免费一级毛片免费播放| 亚洲精品无码久久毛片波多野吉衣| 国产亚洲精品欧洲在线观看| 日韩在线永久免费播放| 凹凸精品视频分类国产品免费| 亚洲国产日韩一区高清在线| 精品亚洲成A人在线观看青青| 在线观看免费av网站| 亚洲无码高清在线观看| 国产精品久久亚洲不卡动漫| 日本视频在线观看永久免费| 免费乱理伦在线播放| 亚洲免费福利视频|