锘??xml version="1.0" encoding="utf-8" standalone="yes"?>久久99国产精品99久久,狠狠色丁香婷婷久久综合,久久综合久久自在自线精品自 http://www.shnenglu.com/ant/category/4993.htmlThe dreams in which I'm dying are the best I've ever had...zh-cnThu, 20 May 2010 06:43:56 GMTThu, 20 May 2010 06:43:56 GMT60鑵捐騫垮窞鎷涜仒鎵嬫満瀹㈡埛绔紑鍙戜漢鍛?/title><link>http://www.shnenglu.com/ant/archive/2010/05/19/115846.html</link><dc:creator>铓傝殎緇堢粨鑰?/dc:creator><author>铓傝殎緇堢粨鑰?/author><pubDate>Wed, 19 May 2010 14:14:00 GMT</pubDate><guid>http://www.shnenglu.com/ant/archive/2010/05/19/115846.html</guid><wfw:comment>http://www.shnenglu.com/ant/comments/115846.html</wfw:comment><comments>http://www.shnenglu.com/ant/archive/2010/05/19/115846.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/ant/comments/commentRss/115846.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/ant/services/trackbacks/115846.html</trackback:ping><description><![CDATA[ <font face="Verdana">濂戒箙娌℃洿鏂版棩蹇椾簡錛屽彂涓鍒欏皬鎷涜仒騫垮憡錛?br /><br />聽聽聽 鑵捐騫垮窞鎷涜仒鎵嬫満瀹㈡埛绔紑鍙戜漢鍛?br />聽聽聽 鍩烘湰瑕佹眰錛?br /><b>聽聽聽 1).1锝?騫村伐浣滅粡楠岋紱<br />聽聽聽 2).鏈塖ymbian/Andriod/iPhone鎵嬫満瀹㈡埛绔紑鍙戠粡楠?鏈塖ymbian C++寮鍙戠粡楠岃呬紭鍏?錛?/b><br /><br />鏈夋剰鍚戣呰鑱旂郴錛歱eterfan@tencent.com</font> <br /> <br /> <img src ="http://www.shnenglu.com/ant/aggbug/115846.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/ant/" target="_blank">铓傝殎緇堢粨鑰?/a> 2010-05-19 22:14 <a href="http://www.shnenglu.com/ant/archive/2010/05/19/115846.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Win32 Service鐨勭畝鍗曞皝瑁?/title><link>http://www.shnenglu.com/ant/archive/2007/09/24/32769.html</link><dc:creator>铓傝殎緇堢粨鑰?/dc:creator><author>铓傝殎緇堢粨鑰?/author><pubDate>Mon, 24 Sep 2007 05:01:00 GMT</pubDate><guid>http://www.shnenglu.com/ant/archive/2007/09/24/32769.html</guid><wfw:comment>http://www.shnenglu.com/ant/comments/32769.html</wfw:comment><comments>http://www.shnenglu.com/ant/archive/2007/09/24/32769.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.shnenglu.com/ant/comments/commentRss/32769.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/ant/services/trackbacks/32769.html</trackback:ping><description><![CDATA[<p>鍓嶆鏃墮棿瀛︿範(fàn)Windows紼嬪簭璁捐錛屽垰濂藉鍒癢in32 Service錛屼簬鏄啓浜嗕袱涓畝鍗曠殑綾伙細(xì)BaseService鍜孲erviceCtrl銆傝櫧鐒跺姛鑳芥瘮杈冪畝鍗曪紝浣嗘槸涔熻兘閫傜敤浜庡ぇ澶氭暟鎯呭喌銆備笅闈粙緇嶄竴涓嬬畝鍗曠敤娉曪紝濡傛灉浣犲垰濂介渶瑕佸啓涓浜涚畝鍗曠殑鏈嶅姟紼嬪簭錛岃繖涓や釜綾諱篃璁歌兘媧句笂鐢ㄥ満錛?br><br><span style="FONT-SIZE: 14pt"><strong style="COLOR: #993366">1. BaseService<br></strong></span><em style="COLOR: #333399">BaseService.h<br></em></p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><font face="Courier New"><span style="COLOR: #008080"> 1</span> </font> <font face="Courier New"><span style="COLOR: #000000">#ifndef BASE_SERVICE_H<br></span><span style="COLOR: #008080"> 2</span> </font> <font face="Courier New"><span style="COLOR: #000000">#define BASE_SERVICE_H<br></span><span style="COLOR: #008080"> 3</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span></font> <font face="Courier New"><span style="COLOR: #000000"> BaseService {<br></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">public</span></font> <font face="Courier New"><span style="COLOR: #000000">:<br></span><span style="COLOR: #008080"> 6</span> </font> <font face="Courier New"><span style="COLOR: #000000">    explicit BaseService(LPCTSTR szServiceName,<br></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000">                DWORD dwServiceType </span><span style="COLOR: #000000">=</span></font> <font face="Courier New"><span style="COLOR: #000000"> SERVICE_WIN32_OWN_PROCESS,<br></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">                DWORD dwStartType </span><span style="COLOR: #000000">=</span></font> <font face="Courier New"><span style="COLOR: #000000"> SERVICE_AUTO_START);<br></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #000000">~</span></font> <font face="Courier New"><span style="COLOR: #000000">BaseService() {}<br></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000">    bool ParseStandardArgs(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> argc, </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span></font> <font face="Courier New"><span style="COLOR: #000000"> argv[]);<br></span><span style="COLOR: #008080">11</span> </font> <font face="Courier New"><span style="COLOR: #000000">    bool IsInstalled();<br></span><span style="COLOR: #008080">12</span> </font> <font face="Courier New"><span style="COLOR: #000000">    bool Install();<br></span><span style="COLOR: #008080">13</span> </font> <font face="Courier New"><span style="COLOR: #000000">    bool Uninstall();<br></span><span style="COLOR: #008080">14</span> </font> <font face="Courier New"><span style="COLOR: #000000">    bool Start();<br></span><span style="COLOR: #008080">15</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">private</span></font> <font face="Courier New"><span style="COLOR: #000000">:<br></span><span style="COLOR: #008080">16</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> Run() </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span></font> <font face="Courier New"><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">17</span> <span style="COLOR: #000000">    virtual bool OnInitialize() { </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span></font> <font face="Courier New"><span style="COLOR: #000000">; }<br></span><span style="COLOR: #008080">18</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #0000ff">void</span></font> <font face="Courier New"><span style="COLOR: #000000"> OnStop() {}<br></span><span style="COLOR: #008080">19</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #0000ff">void</span></font> <font face="Courier New"><span style="COLOR: #000000"> OnPause() {}<br></span><span style="COLOR: #008080">20</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #0000ff">void</span></font> <font face="Courier New"><span style="COLOR: #000000"> OnContinue() {}<br></span><span style="COLOR: #008080">21</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #0000ff">void</span></font> <font face="Courier New"><span style="COLOR: #000000"> OnInterrogate() {}<br></span><span style="COLOR: #008080">22</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #0000ff">void</span></font> <font face="Courier New"><span style="COLOR: #000000"> OnShutdown() {}<br></span><span style="COLOR: #008080">23</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #0000ff">void</span></font> <font face="Courier New"><span style="COLOR: #000000"> OnUserControl(DWORD dwControl) {}<br></span><span style="COLOR: #008080">24</span> </font> <font face="Courier New"><span style="COLOR: #000000">    ...<br></span><span style="COLOR: #008080">25</span> </font> <font face="Courier New"><span style="COLOR: #000000">};<br></span><span style="COLOR: #008080">26</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080">27</span> <span style="COLOR: #000000">#endif</span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">BASE_SERVICE_H</span><span style="COLOR: #008000">*/</span></font> </div> <p>瑕佸疄鐜拌嚜宸辯殑鏈嶅姟綾誨彧闇浠嶣aseService緇ф壙騫朵笖Override鐩稿叧鐨剉irtual鍑芥暟鍗沖彲錛屼笅闈㈢ず鑼冧竴涓狟eepService綾伙紝璇ユ湇鍔″彧鏄畝鍗曞湴姣忛殧2縐抌eep涓涓嬶紝涓轟簡綆鍗曟墍鏈変唬鐮佸潎鏀懼湪.h鏂囦歡涓細(xì)<br><em style="COLOR: #333399">BeepService.h</em></p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><font face="Courier New"><span style="COLOR: #008080"> 1</span> </font> <font face="Courier New"><span style="COLOR: #000000">#ifndef BEEP_SERVICE_H<br></span><span style="COLOR: #008080"> 2</span> </font> <font face="Courier New"><span style="COLOR: #000000">#define BEEP_SERVICE_H<br></span><span style="COLOR: #008080"> 3</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">BaseService.h</span><span style="COLOR: #000000">"</span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 5</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> BeepService : </span><span style="COLOR: #0000ff">public</span></font> <font face="Courier New"><span style="COLOR: #000000"> BaseService {<br></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">public</span></font> <font face="Courier New"><span style="COLOR: #000000">:<br></span><span style="COLOR: #008080"> 8</span> </font> <font face="Courier New"><span style="COLOR: #000000">    BeepService(LPCTSTR szServiceName)<br></span><span style="COLOR: #008080"> 9</span> </font> <font face="Courier New"><span style="COLOR: #000000">        :BaseService(szServiceName)<br></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000">        ,m_bPaused(</span><span style="COLOR: #0000ff">false</span></font> <font face="Courier New"><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000">        ,m_bRunning(</span><span style="COLOR: #0000ff">false</span></font> <font face="Courier New"><span style="COLOR: #000000">) {}<br></span><span style="COLOR: #008080">12</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080">13</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> OnStop() { m_bRunning </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span></font> <font face="Courier New"><span style="COLOR: #000000">; }<br></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> OnPause() { m_bPaused </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span></font> <font face="Courier New"><span style="COLOR: #000000">; }<br></span><span style="COLOR: #008080">15</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> OnContinue() { m_bPaused </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span></font> <font face="Courier New"><span style="COLOR: #000000">; }<br></span><span style="COLOR: #008080">16</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #0000ff">void</span></font> <font face="Courier New"><span style="COLOR: #000000"> Run() {<br></span><span style="COLOR: #008080">17</span> <span style="COLOR: #000000">        m_bRunning </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span></font> <font face="Courier New"><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">18</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">while</span></font> <font face="Courier New"><span style="COLOR: #000000"> (m_bRunning) {<br></span><span style="COLOR: #008080">19</span> <span style="COLOR: #000000">            </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (</span><span style="COLOR: #000000">!</span></font> <font face="Courier New"><span style="COLOR: #000000">m_bPaused)<br></span><span style="COLOR: #008080">20</span> <span style="COLOR: #000000">                Beep(</span><span style="COLOR: #000000">800</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">800</span></font> <font face="Courier New"><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">21</span> <span style="COLOR: #000000">            Sleep(</span><span style="COLOR: #000000">2000</span></font> <font face="Courier New"><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">22</span> </font> <font face="Courier New"><span style="COLOR: #000000">        }<br></span><span style="COLOR: #008080">23</span> </font> <font face="Courier New"><span style="COLOR: #000000">    }<br></span><span style="COLOR: #008080">24</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">private</span></font> <font face="Courier New"><span style="COLOR: #000000">:<br></span><span style="COLOR: #008080">25</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">volatile</span></font> <font face="Courier New"><span style="COLOR: #000000"> bool m_bPaused;<br></span><span style="COLOR: #008080">26</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">volatile</span></font> <font face="Courier New"><span style="COLOR: #000000"> bool m_bRunning;<br></span><span style="COLOR: #008080">27</span> </font> <font face="Courier New"><span style="COLOR: #000000">};<br></span><span style="COLOR: #008080">28</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080">29</span> <span style="COLOR: #000000">#endif</span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">BEEP_SERVICE_H</span><span style="COLOR: #008000">*/</span></font> </div> <p>閫氬父鏉ヨ鍙』瑕丱verride涓婇潰鐨?涓獀irtual鍑芥暟灝監(jiān)K浜嗭細(xì)<br>鍦?em>Run()</em>涓繘琛屽疄闄呯殑宸ヤ綔錛?em>OnStop(),OnPause(),OnContinue()</em>鍒欐槸涓轟簡鍝嶅簲Service Control Manager鐨勬帶鍒躲?br><em style="COLOR: #333399"><br>test.cpp<br></em></p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><font face="Courier New"><span style="COLOR: #008080"> 1</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">windows.h</span><span style="COLOR: #000000">></span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">tchar.h</span><span style="COLOR: #000000">></span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">></span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">BeepService.h</span><span style="COLOR: #000000">"</span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 5</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000"></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">*</span></font> <font face="Courier New"><span style="COLOR: #000000">argv[]) {<br></span><span style="COLOR: #008080"> 7</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">    BeepService beepService(_T(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">BeepService</span><span style="COLOR: #000000">"</span></font> <font face="Courier New"><span style="COLOR: #000000">));<br></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (</span><span style="COLOR: #000000">!</span></font> <font face="Courier New"><span style="COLOR: #000000">beepService.ParseStandardArgs(argc, argv)) {<br></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">if</span></font> <font face="Courier New"><span style="COLOR: #000000"> (beepService.IsInstalled()) {<br></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000">            </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (</span><span style="COLOR: #000000">!</span></font> <font face="Courier New"><span style="COLOR: #000000">beepService.Start())<br></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000">                printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">The service can not run from command line.\n</span><span style="COLOR: #000000">"</span></font> <font face="Courier New"><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">13</span> <span style="COLOR: #000000">        } </span><span style="COLOR: #0000ff">else</span></font> <font face="Courier New"><span style="COLOR: #000000"> {<br></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000">            printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">The service is not installed, </span><span style="COLOR: #000000">"</span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080">15</span> <span style="COLOR: #000000">                </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">use \</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">s </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">i\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> to install.\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, argv[</span><span style="COLOR: #000000">0</span></font> <font face="Courier New"><span style="COLOR: #000000">]);<br></span><span style="COLOR: #008080">16</span> </font> <font face="Courier New"><span style="COLOR: #000000">        }<br></span><span style="COLOR: #008080">17</span> </font> <font face="Courier New"><span style="COLOR: #000000">    }<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: #000000">0</span></font> <font face="Courier New"><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">19</span> <span style="COLOR: #000000">}</span></font> </div> <p>鍋囪緙栬瘧鍚庣敓鎴愮殑exe鏂囦歡涓篵eep.exe錛屽垯鍦ㄥ懡浠よ涓彲浠ュ涓嬩嬌鐢細(xì)<br><em style="COLOR: #333399">(1). beep -i    瀹夎service(瀹夎浠ュ悗緋葷粺榪愯鏃朵細(xì)鑷姩鍚姩)<br>(2). beep -u   鍗歌澆service(濡傛灉service姝e湪榪愯錛屽垯鍏堝仠姝ervice鍐嶅嵏杞?<br></em>BaseServiced 鐨凱arseStandardArgs姝f槸鐢ㄦ潵瑙f瀽涓婅堪涓や釜鍛戒護(hù)銆?br><br><span style="FONT-SIZE: 14pt"><strong style="COLOR: #993366">2. ServiceCtrl<br></strong></span>铏界劧Windows鑷甫鐨凷ervice Control Manager鍙互鎺у埗鏈嶅姟紼嬪簭錛屼絾鏄緢澶氭椂鍊欐垜浠兘闇瑕佺敤浠g爜鎺у埗錛岃繖灝辯敤鍒癝erviceCtrl綾伙紝璇ョ被鐨勬帴鍙e涓嬶細(xì)<br><em style="COLOR: #333399">ServiceCtrl.h</em></p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><font face="Courier New"><span style="COLOR: #008080"> 1</span> </font> <font face="Courier New"><span style="COLOR: #000000">#ifndef SERVICE_CTRL_H<br></span><span style="COLOR: #008080"> 2</span> </font> <font face="Courier New"><span style="COLOR: #000000">#define SERVICE_CTRL_H<br></span><span style="COLOR: #008080"> 3</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span></font> <font face="Courier New"><span style="COLOR: #000000"> ServiceCtrl {<br></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">public</span></font> <font face="Courier New"><span style="COLOR: #000000">:<br></span><span style="COLOR: #008080"> 6</span> </font> <font face="Courier New"><span style="COLOR: #000000">    ServiceCtrl(LPCTSTR szServiceName);<br></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000">    </span><span style="COLOR: #000000">~</span></font> <font face="Courier New"><span style="COLOR: #000000">ServiceCtrl();<br></span><span style="COLOR: #008080"> 8</span> </font> <font face="Courier New"><span style="COLOR: #000000">    bool Start();<br></span><span style="COLOR: #008080"> 9</span> </font> <font face="Courier New"><span style="COLOR: #000000">    bool Pause();<br></span><span style="COLOR: #008080">10</span> </font> <font face="Courier New"><span style="COLOR: #000000">    bool Continue();<br></span><span style="COLOR: #008080">11</span> </font> <font face="Courier New"><span style="COLOR: #000000">    bool Stop();<br></span><span style="COLOR: #008080">12</span> </font> <font face="Courier New"><span style="COLOR: #000000">    bool Interrogate();<br></span><span style="COLOR: #008080">13</span> </font> <font face="Courier New"><span style="COLOR: #000000">    bool UserControl(DWORD dwControl);<br></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000">    DWORD State() </span><span style="COLOR: #0000ff">const</span></font> <font face="Courier New"><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">15</span> </font> <font face="Courier New"><span style="COLOR: #000000">    ...<br></span><span style="COLOR: #008080">16</span> </font> <font face="Courier New"><span style="COLOR: #000000">};<br></span><span style="COLOR: #008080">17</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080">18</span> <span style="COLOR: #000000">#endif</span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">SERVICE_CTRL_H</span><span style="COLOR: #008000">*/</span></font> </div> <p>鎺ュ彛姣旇緝鐩磋娌′粈涔堝ソ璇寸殑錛岀湅涓嬮潰鐨勭ず渚嬩唬鐮侊細(xì)<br><em><font color=#333399>test.cpp</font></em></p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><font face="Courier New"><span style="COLOR: #008080"> 1</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">windows.h</span><span style="COLOR: #000000">></span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">tchar.h</span><span style="COLOR: #000000">></span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">></span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">exception</span><span style="COLOR: #000000">></span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">BeepService.h</span><span style="COLOR: #000000">"</span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">ServiceCtrl.h</span><span style="COLOR: #000000">"</span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 7</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000"></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">*</span></font> <font face="Courier New"><span style="COLOR: #000000">argv[]) {<br></span><span style="COLOR: #008080"> 9</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080">10</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">try</span></font> <font face="Courier New"><span style="COLOR: #000000"> {<br></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000">        ServiceCtrl servCtrl(_T(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">BeepService</span><span style="COLOR: #000000">"</span></font> <font face="Courier New"><span style="COLOR: #000000">));<br></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (servCtrl.State() </span><span style="COLOR: #000000">!=</span></font> <font face="Courier New"><span style="COLOR: #000000"> SERVICE_STOPPED) {<br></span><span style="COLOR: #008080">13</span> <span style="COLOR: #000000">            printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Service already started.\n</span><span style="COLOR: #000000">"</span></font> <font face="Courier New"><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000">        } </span><span style="COLOR: #0000ff">else</span></font> <font face="Courier New"><span style="COLOR: #000000"> {<br></span><span style="COLOR: #008080">15</span> </font> <font face="Courier New"><span style="COLOR: #000000">            servCtrl.Start();<br></span><span style="COLOR: #008080">16</span> <span style="COLOR: #000000">            printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Start.\n</span><span style="COLOR: #000000">"</span></font> <font face="Courier New"><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">17</span> <span style="COLOR: #000000">            Sleep(</span><span style="COLOR: #000000">6000</span></font> <font face="Courier New"><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">18</span> </font> <font face="Courier New"><span style="COLOR: #000000">            servCtrl.Pause();<br></span><span style="COLOR: #008080">19</span> <span style="COLOR: #000000">            printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Pause.\n</span><span style="COLOR: #000000">"</span></font> <font face="Courier New"><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">20</span> <span style="COLOR: #000000">            Sleep(</span><span style="COLOR: #000000">6000</span></font> <font face="Courier New"><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">21</span> </font> <font face="Courier New"><span style="COLOR: #000000">            servCtrl.Continue();<br></span><span style="COLOR: #008080">22</span> <span style="COLOR: #000000">            printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Continue.\n</span><span style="COLOR: #000000">"</span></font> <font face="Courier New"><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">23</span> <span style="COLOR: #000000">            Sleep(</span><span style="COLOR: #000000">6000</span></font> <font face="Courier New"><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">24</span> </font> <font face="Courier New"><span style="COLOR: #000000">            servCtrl.Stop();<br></span><span style="COLOR: #008080">25</span> <span style="COLOR: #000000">            printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Stop.\n</span><span style="COLOR: #000000">"</span></font> <font face="Courier New"><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">26</span> </font> <font face="Courier New"><span style="COLOR: #000000">        }<br></span><span style="COLOR: #008080">27</span> <span style="COLOR: #000000">    } </span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000"> (std::exception </span><span style="COLOR: #000000">&</span></font> <font face="Courier New"><span style="COLOR: #000000">e) {<br></span><span style="COLOR: #008080">28</span> <span style="COLOR: #000000">        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s\n</span><span style="COLOR: #000000">"</span></font> <font face="Courier New"><span style="COLOR: #000000">, e.what());<br></span><span style="COLOR: #008080">29</span> </font> <font face="Courier New"><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></font> <font face="Courier New"><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">31</span> <span style="COLOR: #000000">}</span></font> </div> <br>婧愪唬鐮侊細(xì)<a title=鐐瑰嚮涓嬭澆 href="http://www.shnenglu.com/Files/ant/Service.rar">鐐瑰嚮涓嬭澆</a><br> <img src ="http://www.shnenglu.com/ant/aggbug/32769.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/ant/" target="_blank">铓傝殎緇堢粨鑰?/a> 2007-09-24 13:01 <a href="http://www.shnenglu.com/ant/archive/2007/09/24/32769.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>浠嶹in32 API灝佽Thread綾籟2]http://www.shnenglu.com/ant/archive/2007/08/31/31280.html铓傝殎緇堢粨鑰?/dc:creator>铓傝殎緇堢粨鑰?/author>Fri, 31 Aug 2007 03:58:00 GMThttp://www.shnenglu.com/ant/archive/2007/08/31/31280.htmlhttp://www.shnenglu.com/ant/comments/31280.htmlhttp://www.shnenglu.com/ant/archive/2007/08/31/31280.html#Feedback6http://www.shnenglu.com/ant/comments/commentRss/31280.htmlhttp://www.shnenglu.com/ant/services/trackbacks/31280.html(functor聽and function object)浣滀負(fù)鏋勯犲嚱鏁板弬鏁般傚鏋滀綘鐔熸?zhèn)塖TL錛屼綘搴旇鐔熸?zhèn)塨ind1st鍜宐ind2nd榪欎袱涓嚱鏁伴厤鎺ュ櫒(function adapter)錛宐ind1st鍜宐ind2nd鍙互灝嗕竴涓簩鍏冨嚱鏁?binary function)杞崲鎴愪竴鍏冨嚱鏁?unary function)銆備負(fù)浜嗕嬌Thread綾昏兘澶熺敤鏅氬嚱鏁板拰鍑芥暟瀵硅薄浣滀負(fù)鍙傛暟錛屾垜浠渶瑕佷竴涓猙ind灝嗕竴鍏冨嚱鏁拌漿鎹㈡垚鏃犲弬鍑芥暟錛?br />bind.h
聽1#ifndef聽BIND_H
聽2
#define聽BIND_H
聽3

聽4template聽<class聽_Result>
聽5struct聽trivial_function聽{
聽6
聽聽聽聽typedef聽_Result聽result_type;
聽7
};
聽8

聽9template聽<class聽_Operation>
10class聽binder聽:聽public聽trivial_function<typename聽_Operation::result_type>聽{
11public
:
12聽聽聽聽binder(const聽_Operation& x,聽const聽typename聽_Operation::argument_type&
y)
13
聽聽聽聽聽聽聽聽:op(x),聽value(y)聽{}
14聽聽聽聽typename聽_Operation::result_type聽operator()()聽const
聽{
15聽聽聽聽聽聽聽聽return
聽op(value);
16
聽聽聽聽}
17protected
:
18
聽聽聽聽_Operation聽op;
19
聽聽聽聽typename聽_Operation::argument_type聽value;
20
};
21

22template聽<class聽_Operation,聽class聽_Tp>
23inline聽binder<_Operation>
24bind(const聽_Operation&聽__fn,聽const聽_Tp&
聽__x)聽
25
{
26
聽聽聽聽typedef聽typename聽_Operation::argument_type聽_Arg_type;
27聽聽聽聽return聽binder<_Operation>
(__fn,聽_Arg_type(__x));
28
}
29

30#endif/*BIND_H*/

鏈変簡bind鎴戜滑榪橀渶瑕佷慨鏀筎hread綾葷殑鏋勯犲嚱鏁幫紝鏄劇劧鎴戜滑蹇呴』灝嗘瀯閫犲嚱鏁板0鏄庝負(fù)鎴愬憳妯℃澘(榪樻湁涓縐嶆柟娉曚篃鍙互杈懼埌鍚屾牱鐨勭洰鐨勶紝灝辨槸鎶奣hread綾誨0鏄庝負(fù)妯℃澘錛屼絾鏄繖鏍風(fēng)殑璁捐濂藉儚涓嶅お濂?錛岃繖鏍鋒墠鑳藉璁㏕hread綾葷殑鏋勯犲嚱鏁板彲浠ユ帴鍙楀悇縐嶇被鍨嬬殑鍙傛暟錛屼慨鏀瑰悗鐨勬瀯閫犲嚱鏁板簲璇ヨ兘澶熶嬌鐢ㄥ涓嬩笁縐嶇被鍨嬬殑鍙傛暟錛?br />1.Runnable *
2.no argument function
3.no argument functor
涓嬮潰鏄慨鏀瑰悗鐨勫ご鏂囦歡錛?br />runnable.h
聽1#ifndef聽RUNNABLE_H
聽2
#define聽RUNNABLE_H
聽3

聽4struct聽Runnable聽{
聽5聽聽聽聽virtual聽void聽run()聽=0
;
聽6聽聽聽聽virtual聽~
Runnable()聽{}
聽7
};
聽8

聽9template聽<class聽T>
10class聽RunnableFunctor聽:聽public聽Runnable聽{
11public
:
12聽聽聽聽RunnableFunctor(const聽T&
f)聽:_func(f)聽{}
13聽聽聽聽virtual聽void
聽run()聽{聽_func();聽}
14private
:
15
聽聽聽聽T聽_func;
16
};
17

18//base聽template聽for聽no聽argument聽functor
19template聽<class聽T>
20struct聽FuncImpl聽{
21聽聽聽聽static聽Runnable*聽transfer(const聽T&
t)聽{
22聽聽聽聽聽聽聽聽returnnew聽RunnableFunctor<T>
(t);
23
聽聽聽聽}
24
};
25

26//partial聽specialization聽for聽T*
27template聽<class聽T>
28struct聽FuncImpl<T*>聽{
29聽聽聽聽static聽Runnable*聽transfer(T*
t)聽{
30聽聽聽聽聽聽聽聽return
聽t;
31
聽聽聽聽}
32
};
33

34//partial聽specialization聽for聽no聽argument聽function
35template聽<class聽T>
36struct聽FuncImpl<T聽(*)()>聽{
37聽聽聽聽static聽Runnable*聽transfer(T聽(*
t)())聽{
38聽聽聽聽聽聽聽聽returnnew聽RunnableFunctor<T聽(*)()>
(t);
39
聽聽聽聽}
40
};
41

42template聽<class聽T>
43inline聽Runnable*聽transfer(const聽T& t)聽{
44聽聽聽聽return聽FuncImpl<T>
::transfer(t);
45
}
46

47#endif/*RUNNABLE_H*/

thread.h
聽1#ifndef聽THREAD_H
聽2
#define聽THREAD_H
聽3

聽4#include聽<windows.h>
聽5#include聽"bind.h"
聽6#include聽"runnable.h"
聽7
聽8#define聽CLASS_UNCOPYABLE(classname)聽\
聽9聽聽聽聽private
:聽\
10聽聽聽聽classname(const聽classname&
);聽\
11聽聽聽聽classname&聽operator=(const聽classname&
);
12

13class聽Thread聽:聽public聽Runnable聽{
14
聽聽聽聽CLASS_UNCOPYABLE(Thread)
15public
:
16
聽聽聽聽Thread()
17聽聽聽聽聽聽聽聽:_target(0
)
18聽聽聽聽聽聽聽聽,_handle(0
)聽{
19

20聽聽聽聽}
21聽聽聽聽template聽<class聽T>

22聽聽聽聽explicit聽Thread(const聽T& op)
23
聽聽聽聽聽聽聽聽:_target(transfer(op))
24聽聽聽聽聽聽聽聽,_handle(0
)聽{
25

26聽聽聽聽}
27聽聽聽聽virtual聽~
Thread();
28聽聽聽聽virtual聽void
聽run()聽{}
29聽聽聽聽void
聽start();
30聽聽聽聽void
聽join();
31private
:
32聽聽聽聽static聽unsigned聽__stdcall聽threadProc(void*
param);
33private
:
34聽聽聽聽Runnable*
_target;
35
聽聽聽聽HANDLE聽_handle;
36
};
37

38#endif/*THREAD_H*/

thread.cpp鍜屽墠涓綃囩殑鍑犱箮涓鏍鳳紝鍞竴鐨勪笉鍚屾槸鍘繪帀浜嗘瀯閫犲嚱鏁癟hread(Runnable *)錛屽洜涓虹幇鍦ㄧ殑鏋勯犲嚱鏁版敼鎴愪簡鎴愬憳妯℃澘錛屽疄鐜頒篃鏀懼湪thread.h涓簡銆傜幇鍦ㄧ殑鏋勯犲嚱鏁拌兘澶熸帴鍙楀悇縐嶇被鍨嬬殑鍙傛暟錛屼富瑕佸綊鍔熶簬妯℃澘鍑芥暟transfer錛屽疄鐜頒唬鐮佸湪runnable.h涓紝涓昏鎶宸ф槸鐢ㄧ被鐨勫亸鐗瑰寲妯℃嫙鍑芥暟妯℃澘鐨勫亸鐗瑰寲錛屼笉鏄庣櫧鐨勮鐪?a id="_117bf2a46b69_HomePageDays_DaysList_ctl00_DayItem_DayList_ctl01_TitleUrl" href="/ant/archive/2007/08/30/31201.html">涓轟粈涔堜笉瑕佺壒鍖栧嚱鏁版ā鐗?/a>銆?br />
涓嬮潰鏄祴璇曚唬鐮侊細(xì)
test.cpp
聽1#include聽"thread.h"
聽2#include聽<iostream>
聽3#include聽<functional>
聽4
聽5using聽namespace聽std;
聽6

聽7//no聽argument聽function
聽8void聽print()聽{
聽9聽聽聽聽cout聽<<"print"<<
聽endl;
10
}
11

12//unary聽function
13void聽print1(int聽n)聽{
14聽聽聽聽cout聽<<"print1"<<
聽endl;
15
}
16

17//binary聽function
18void聽print2(int聽m,聽int聽n)聽{
19聽聽聽聽cout聽<<"print2"<<
聽endl;
20
}
21

22
23//no聽argument聽functor
24struct聽PrintFunctor聽{
25聽聽聽聽void聽operator()()聽const
聽{
26聽聽聽聽聽聽聽聽cout聽<<"PrintFunctor"<<
聽endl;
27
聽聽聽聽}
28
};
29

30//unary聽functor
31struct聽PrintFunctor1聽:聽public聽unary_function<int,聽void>聽{
32聽聽聽聽void聽operator()(int聽n)聽const
聽{
33聽聽聽聽聽聽聽聽cout聽<<"PrintFunctor1"<<
聽endl;
34
聽聽聽聽}
35
};
36

37//binary聽functor
38struct聽PrintFunctor2聽:聽public聽binary_function<int,聽int,聽void>聽{
39聽聽聽聽void聽operator()(int聽m,聽int聽n)聽const
聽{
40聽聽聽聽聽聽聽聽cout聽<<"PrintFunctor2"<<
聽endl;
41
聽聽聽聽}
42
};
43

44int聽main()聽{
45

46聽聽聽聽//construct聽Thread聽with聽no聽argument聽function
47聽聽聽聽Thread聽thread1(&print);
48
聽聽聽聽thread1.start();
49

50聽聽聽聽//construct聽Thread聽with聽unary聽function
51聽聽聽聽Thread聽thread2(bind(ptr_fun(print1),聽5));
52
聽聽聽聽thread2.start();
53

54聽聽聽聽//construct聽Thread聽with聽binary聽function
55聽聽聽聽Thread聽thread3(bind(bind1st(ptr_fun(print2),聽1),聽2));
56
聽聽聽聽thread3.start();
57

58
59聽聽聽聽//construct聽Thread聽with聽no聽argument聽functor
60聽聽聽聽Thread聽thread4((PrintFunctor()));
61
聽聽聽聽thread4.start();
62

63聽聽聽聽//construct聽Thread聽with聽unary聽functor
64聽聽聽聽Thread聽thread5(bind(PrintFunctor1(),聽5));
65
聽聽聽聽thread5.start();
66

67聽聽聽聽//construct聽Thread聽with聽binary聽functor
68聽聽聽聽Thread聽thread6(bind(bind1st(PrintFunctor2(),聽1),聽2));
69
聽聽聽聽thread6.start();
70

71聽聽聽聽thread1.join();
72
聽聽聽聽thread2.join();
73
聽聽聽聽thread3.join();
74
聽聽聽聽thread4.join();
75
聽聽聽聽thread5.join();
76
聽聽聽聽thread6.join();
77

78聽聽聽聽return0;
79}

褰撶劧浜嗭紝涓婇潰鐨勫茍涓嶆槸鍏ㄩ儴錛屼慨鏀瑰悗鐨凾hread綾諱笉浠呰兘澶熶嬌鐢ㄥ師鍏堢殑浠嶵hread綾葷戶鎵挎垨鑰呭疄鐜癛unnable鎺ュ彛鐨勬柟娉曪紝榪樺彲浠ヤ嬌鐢ㄤ換浣曟棤鍙傚嚱鏁版垨鏃犲弬鍑芥暟瀵硅薄銆傞櫎浜唗est.cpp閲岀ず鑼冪殑錛屼綘鐢氳嚦鍙互鐢╞ind,bind1st,bind2st,mem_fun,mem_fun_ref鐨勭粍鍚堟潵鐢ㄦ煇涓被鐨勬垚鍛樺嚱鏁頒綔涓哄弬鏁幫紝鍏鋒湁瓚呭己鐨勭伒媧繪с?br />
鐩墠瀹炵幇鐨勮繖浜涢兘鏄疶hread綾繪渶鍩烘湰鐨勫姛鑳斤紝鍏朵粬鍔熻兘濡傝緗嚎紼嬩紭鍏堢駭錛屾寕璧鋒垨鎭㈠綰跨▼錛屽紓甯稿鐞嗙瓑鍏蜂綋瀹炵幇閮芥瘮杈冪畝鍗曪紝榪欒繖閲屽氨涓嶄竴涓瀹炵幇浜嗐?
婧愪唬鐮佷笅杞斤細(xì)鐐瑰嚮涓嬭澆


]]>
C++&Win32鍐欑殑絀哄綋鎺ラ緳http://www.shnenglu.com/ant/archive/2007/08/30/31234.html铓傝殎緇堢粨鑰?/dc:creator>铓傝殎緇堢粨鑰?/author>Thu, 30 Aug 2007 13:37:00 GMThttp://www.shnenglu.com/ant/archive/2007/08/30/31234.htmlhttp://www.shnenglu.com/ant/comments/31234.htmlhttp://www.shnenglu.com/ant/archive/2007/08/30/31234.html#Feedback18http://www.shnenglu.com/ant/comments/commentRss/31234.htmlhttp://www.shnenglu.com/ant/services/trackbacks/31234.html絀哄綋鎺ラ緳娓告垙錛屽啓浜嗕竴涓ぜ鎷滐紝瀹屽叏浠縲indows鐨勫懙鍛點(diǎn)?br>涓嶈繃涔熶笉鍏ㄤ竴鏍鳳紝鏈変竴浜涚粏鑺備竴鐩存病鏈夋椂闂村紕錛屾病鍔炴硶鏈榪戞瘮杈冩噿...

涓巜indows涓嬬殑鐩告瘮錛屾垜鍋氱殑榪欎釜鍔犲叆浜?span style="COLOR: #993300">AI(蹇嵎閿瓼6)錛屽彲浠ュ紑鍚?span style="COLOR: #993300">縐葷墝鎻愮ず(蹇嵎閿瓼7)錛屼笉榪?span style="COLOR: #993300">鎾ら攢涓鐩存病鏈夊仛錛屽彲鑳藉摢澶╃獊鐒舵兂鍋氫簡浼?xì)鍔犱笂鍘汇傚彟澶栨敼榪涗簡搴熺墝鍥炴敹綆楁硶錛岄潪娉曠Щ鐗岀殑鎻愮ず鐩存帴鍘繪帀浜嗭紝榪樻湁涓浜涘皬鐨勭粏鑺備綔浜嗘敼榪涖?br>鎰熷叴瓚g殑鍙互涓嬭澆錛?a title=鐐瑰嚮涓嬭澆 href="http://www.shnenglu.com/Files/ant/Freecell.rar">鐐瑰嚮涓嬭澆
婧愪唬鐮佷笅杞斤細(xì)鐐瑰嚮涓嬭澆
欏轟究鍙戝嚑寮犳埅鍥撅細(xì)







]]>
涓轟粈涔堜笉瑕佺壒鍖栧嚱鏁版ā鐗?/title><link>http://www.shnenglu.com/ant/archive/2007/08/30/31201.html</link><dc:creator>铓傝殎緇堢粨鑰?/dc:creator><author>铓傝殎緇堢粨鑰?/author><pubDate>Thu, 30 Aug 2007 05:55:00 GMT</pubDate><guid>http://www.shnenglu.com/ant/archive/2007/08/30/31201.html</guid><wfw:comment>http://www.shnenglu.com/ant/comments/31201.html</wfw:comment><comments>http://www.shnenglu.com/ant/archive/2007/08/30/31201.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.shnenglu.com/ant/comments/commentRss/31201.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/ant/services/trackbacks/31201.html</trackback:ping><description><![CDATA[ <p> <font face="Verdana" color="#006666">Overloading vs. Specialization</font> <br /> <br />鍦–++涓湁<em>class templates</em> 鍜?em>function templates</em>錛岃繖涓ょ妯$増鏈夊緢澶氬尯鍒紝鏈閲嶈鐨勫尯鍒氨鏄噸杞?overloading)錛?br />鏅氱殑C++綾諱笉鑳介噸杞斤紝褰撶劧綾繪ā鐗堜篃涓嶈兘閲嶈澆錛涚浉鍙嶏紝鏅氬嚱鏁板彲浠ラ噸杞斤紝鍑芥暟妯$増涔熻兘閲嶈澆銆傝繖鍐嶆甯鎬笉榪囷紝鐪嬩笅闈㈢殑浠g爜錛?/p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <font face="Courier New"> <span style="COLOR: #008080">聽1</span>聽<span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽Example聽1:聽Class聽vs.聽function聽template,聽and聽overloading聽<br /></span> <span style="COLOR: #008080">聽2</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽3</span>聽</font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽4</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽A聽class聽template</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽5</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽X聽{聽</span><span style="COLOR: #008000">/*...</span><span style="COLOR: #008000"></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000">聽};聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽(a)<br /></span> <span style="COLOR: #008080">聽6</span>聽</font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽7</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽A聽function聽template聽with聽two聽overloads</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽8</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">聽f(聽T聽);聽聽聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽(b)</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽9</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">聽f(聽</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">,聽T,聽</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">聽);聽</span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽(c)<br /></span> <span style="COLOR: #008080">10</span>聽<span style="COLOR: #008000"></span></font> </div> <p>鍍忎笂闈㈡湭鐗瑰寲鐨勬ā鏉塊氬父鍙仛<em>base templates</em>銆傚綋鐒訛紝<em>base templates</em>鑳藉琚壒鍖栵紝鍦ㄧ壒鍖栬繖涓鐐逛笂<br /><em>class templates</em> 鍜?em>function templates</em>鏈夊緢澶х殑鍖哄埆錛氫竴涓?em>class template</em> 鑳藉琚?em>partially specialized</em> and/or <br /><em>fully specialized</em>錛屼竴涓?em>function template</em>鍙兘琚?em>fully specialized</em>錛屼絾鏄敱浜?em>function template</em>s鑳藉閲嶈澆鎴戜滑鍙互閫氳繃閲嶈澆鏉ュ疄鐜板拰<em>partially specialized</em> 鐩稿綋鐨勫姛鑳姐備笅闈㈢殑浠g爜璇存槑浜嗚繖浜涘尯鍒細(xì)</p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <font face="Courier New"> <span style="COLOR: #008080">聽1</span>聽<span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽Example聽1,聽continued:聽Specializing聽templates聽<br /></span> <span style="COLOR: #008080">聽2</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽3</span>聽</font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽4</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽A聽partial聽specialization聽of聽(a)聽for聽pointer聽types聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽5</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽X</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">T</span><span style="COLOR: #000000">*></span><span style="COLOR: #000000">聽{聽</span><span style="COLOR: #008000">/*...</span><span style="COLOR: #008000"></span><span style="COLOR: #008000">*/</span></font> <font face="Courier New"> <span style="COLOR: #000000">聽};<br /></span> <span style="COLOR: #008080">聽6</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽7</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽A聽full聽specialization聽of聽(a)聽for聽int聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽8</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template</span><span style="COLOR: #000000"><></span><span style="COLOR: #000000">聽</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽X</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽{聽</span><span style="COLOR: #008000">/*...</span><span style="COLOR: #008000"></span><span style="COLOR: #008000">*/</span></font> <font face="Courier New"> <span style="COLOR: #000000">聽};<br /></span> <span style="COLOR: #008080">聽9</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">10</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽A聽separate聽base聽template聽that聽overloads聽(b)聽and聽(c)聽<br /></span> <span style="COLOR: #008080">11</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽--聽NOT聽a聽partial聽specialization聽of聽(b),聽because聽<br /></span> <span style="COLOR: #008080">12</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽there's聽no聽such聽thing聽as聽a聽partial聽specialization聽<br /></span> <span style="COLOR: #008080">13</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽of聽a聽function聽template!聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">14</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">聽f(聽T</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">聽);聽聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽(d)<br /></span> <span style="COLOR: #008080">15</span>聽</font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">16</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽A聽full聽specialization聽of聽(b)聽for聽int聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">17</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template</span><span style="COLOR: #000000"><></span><span style="COLOR: #000000">聽</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">聽f</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">(聽</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">聽);聽聽聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽(e)<br /></span> <span style="COLOR: #008080">18</span>聽</font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">19</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽A聽plain聽old聽function聽that聽happens聽to聽overload聽with聽<br /></span> <span style="COLOR: #008080">20</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽(b),聽(c),聽and聽(d)聽--聽but聽not聽(e),聽which聽we'll聽<br /></span> <span style="COLOR: #008080">21</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽discuss聽in聽a聽moment聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">22</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">聽f(聽</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">聽);聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽(f)<br /></span> <span style="COLOR: #008080">23</span>聽<span style="COLOR: #008000"></span></font> </div> <p>鏍規(guī)嵁鍑芥暟閲嶈澆瑙f瀽瑙勫垯錛?/p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <font face="Courier New"> <span style="COLOR: #008080">聽1</span>聽<span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽Example聽1,聽continued:聽Overload聽resolution聽<br /></span> <span style="COLOR: #008080">聽2</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽3</span>聽<span style="COLOR: #008000"></span></font> <font face="Courier New"> <span style="COLOR: #000000">bool聽b;聽<br /></span> <span style="COLOR: #008080">聽4</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span></font> <font face="Courier New"> <span style="COLOR: #000000">聽i;聽<br /></span> <span style="COLOR: #008080">聽5</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">double</span></font> <font face="Courier New"> <span style="COLOR: #000000">聽d;<br /></span> <span style="COLOR: #008080">聽6</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽7</span>聽<span style="COLOR: #000000">f(聽b聽);聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽calls聽(b)聽with聽T聽=聽bool聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽8</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">f(聽i,聽</span><span style="COLOR: #000000">42</span><span style="COLOR: #000000">,聽d聽);聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽calls聽(c)聽with聽T聽=聽int聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽9</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">f(聽</span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">i聽);聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽calls聽(d)聽with聽T聽=聽int聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">10</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">f(聽i聽);聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽calls聽(e)聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">11</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">f(聽d聽);聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽calls聽(f)</span></font> </div> <p>涓婇潰璇寸殑榪欎簺鍏跺疄閮芥槸寰堢畝鍗曠殑鎯呭喌錛屽ぇ澶氭暟浜哄緢瀹規(guī)槗灝辮兘鏄庣櫧錛屼笅闈㈢殑鎵嶆槸瀹規(guī)槗璁╀漢寮勬販鐨勶細(xì)<br /><br />1.鑰冭檻濡備笅浠g爜錛?/p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <font face="Courier New"> <span style="COLOR: #008080">聽1</span>聽<span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽Example聽2:聽Explicit聽specialization聽<br /></span> <span style="COLOR: #008080">聽2</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽3</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽(a)聽a聽base聽template聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽4</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #0000ff">void</span></font> <font face="Courier New"> <span style="COLOR: #000000">聽f(聽T聽);<br /></span> <span style="COLOR: #008080">聽5</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽6</span>聽<span style="COLOR: #000000">template</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽(b)聽a聽second聽base聽template,聽overloads聽(a)聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽7</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">聽f(聽T</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">聽);聽聽聽聽聽</span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽聽聽聽聽(function聽templates聽can't聽be聽partially聽<br /></span> <span style="COLOR: #008080">聽8</span>聽<span style="COLOR: #008000">聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽聽聽聽聽specialized;聽they聽overload聽instead)</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽9</span>聽<span style="COLOR: #008000"></span></font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">10</span>聽<span style="COLOR: #000000">template</span><span style="COLOR: #000000"><></span><span style="COLOR: #000000">聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽(c)聽explicit聽specialization聽of聽(b)聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">11</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">聽f</span><span style="COLOR: #000000"><></span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">*</span></font> <font face="Courier New"> <span style="COLOR: #000000">);<br /></span> <span style="COLOR: #008080">12</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">13</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽...</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">14</span>聽<span style="COLOR: #008000"></span></font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">15</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">聽</span><span style="COLOR: #000000">*</span></font> <font face="Courier New"> <span style="COLOR: #000000">p;聽<br /></span> <span style="COLOR: #008080">16</span>聽<span style="COLOR: #000000">f(聽p聽);聽聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽calls聽(c)</span></font> </div> <p>鏈鍚庝竴琛岀殑緇撴灉鍍忓ぇ澶氭暟浜烘墍鏈熸湜鐨勪竴鏍鳳紝闂鏄細(xì)涓轟粈涔堟湡鏈涙槸榪欎釜緇撴灉錛?br />濡傛灉浣犳湡鏈涚殑鍘熷洜鏄敊璇殑錛屾帴涓嬫潵鐨勪竴瀹氫細(xì)璁╀綘濂藉銆備篃璁鎬綘浼?xì)璇村Q?鎴戜負(fù)<span style="COLOR: #3366ff">int*</span>鍐欎簡涓涓壒鍖栫増鏈紝<span style="COLOR: #3366ff">f(p)</span>褰撶劧浼?xì)璋冪?span style="COLOR: #3366ff">c</span>"錛屼笉騫哥殑鏄紝榪欐鏄敊璇殑鍘熷洜!!!<br /><br />2.鍐嶈冭檻涓嬮潰鐨勪唬鐮侊細(xì)</p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <font face="Courier New"> <span style="COLOR: #008080">聽1</span>聽<span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽Example聽3<br /></span> <span style="COLOR: #008080">聽2</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽3</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽(a)聽same聽old聽base聽template聽as聽before聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽4</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #0000ff">void</span></font> <font face="Courier New"> <span style="COLOR: #000000">聽f(聽T聽);<br /></span> <span style="COLOR: #008080">聽5</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽6</span>聽<span style="COLOR: #000000">template</span><span style="COLOR: #000000"><></span><span style="COLOR: #000000">聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽(c)聽explicit聽specialization,聽this聽time聽of聽(a)</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽7</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">聽f</span><span style="COLOR: #000000"><></span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">*</span></font> <font face="Courier New"> <span style="COLOR: #000000">);<br /></span> <span style="COLOR: #008080">聽8</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽9</span>聽<span style="COLOR: #000000">template</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽(b)聽a聽second聽base聽template,聽overloads聽(a)聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">10</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">聽f(聽T</span><span style="COLOR: #000000">*</span></font> <font face="Courier New"> <span style="COLOR: #000000">聽);<br /></span> <span style="COLOR: #008080">11</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">12</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽...</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">13</span>聽<span style="COLOR: #008000"></span></font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">14</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">聽</span><span style="COLOR: #000000">*</span></font> <font face="Courier New"> <span style="COLOR: #000000">p;聽<br /></span> <span style="COLOR: #008080">15</span>聽<span style="COLOR: #000000">f(聽p聽);聽聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽calls聽(b)!聽overload聽resolution聽ignores聽<br /></span> <span style="COLOR: #008080">16</span>聽<span style="COLOR: #008000">聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽specializations聽and聽operates聽on聽the聽base聽<br /></span> <span style="COLOR: #008080">17</span>聽<span style="COLOR: #008000">聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽function聽templates聽only</span></font> </div> <p>濡傛灉榪欎釜緇撴灉璁╀綘鎰熷埌鎯婂錛岄偅灝卞浜嗭紒寰堝浜洪兘浼?xì)鎰熷埌鎯婂锛?br />鐞嗚В榪欎釜鐨勫叧閿槸錛?em style="COLOR: #993300">Specializations don't overload,only the base templates overload.<br /></em><br />閲嶈澆瑙f瀽浠呬粎閫夋嫨base template(鎴栬卬ontemplate function錛屽鏋滄湁鐨勮瘽)錛屽彧鏈夊綋緙栬瘧鍣ㄥ凡緇忓喅瀹氫簡鍝釜<br />base template灝嗕細(xì)琚夋嫨錛岀紪璇戝櫒鎵嶄細(xì)緇х畫寰涓嬪鎵鵑傚悎鐨勭壒鍖栫増鏈紝濡傛灉鎵懼埌浜嗗氨浣跨敤閭d釜鐗瑰寲鐗堟湰銆?br /><br />鏈鍚庯紝搴斿綋閬垮厤鐗瑰寲鍑芥暟妯℃澘錛屼篃瑕侀伩鍏嶉噸杞藉嚱鏁版ā鏉?nontemplate function鐨勯噸杞藉綋鐒舵病闂)銆傚鏋滀竴瀹氳榪欐牱錛屽彲浠ヤ嬌鐢ㄥ涓嬫柟娉曟ā鎷熷嚱鏁版ā鏉跨殑鍋忕壒鍖栵細(xì)</p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <font face="Courier New"> <span style="COLOR: #008080">聽1</span>聽<span style="COLOR: #008000">//</span><span style="COLOR: #008000">base聽template聽class,聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽2</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template聽</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span></font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽3</span>聽</font> <font face="Courier New"> <span style="COLOR: #000000">struct聽FuncImpl聽{<br /></span> <span style="COLOR: #008080">聽4</span>聽<span style="COLOR: #000000">聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">users,聽go聽ahead聽and聽specialize聽this</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽5</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">聽聽聽聽</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">聽</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">聽apply(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">聽T聽</span><span style="COLOR: #000000">&</span></font> <font face="Courier New"> <span style="COLOR: #000000">t)聽{<br /></span> <span style="COLOR: #008080">聽6</span>聽<span style="COLOR: #000000">聽聽聽聽聽聽聽聽</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">聽</span><span style="COLOR: #000000">0</span></font> <font face="Courier New"> <span style="COLOR: #000000">;<br /></span> <span style="COLOR: #008080">聽7</span>聽</font> <font face="Courier New"> <span style="COLOR: #000000">聽聽聽聽}<br /></span> <span style="COLOR: #008080">聽8</span>聽</font> <font face="Courier New"> <span style="COLOR: #000000">};<br /></span> <span style="COLOR: #008080">聽9</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">10</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">partial聽specialazation聽for聽int</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">11</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template聽</span><span style="COLOR: #000000"><></span></font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">12</span>聽<span style="COLOR: #000000">struct聽FuncImpl</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span></font> <font face="Courier New"> <span style="COLOR: #000000">聽{<br /></span> <span style="COLOR: #008080">13</span>聽<span style="COLOR: #000000">聽聽聽聽</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">聽</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">聽apply(</span><span style="COLOR: #0000ff">int</span></font> <font face="Courier New"> <span style="COLOR: #000000">聽t)聽{<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">1</span></font> <font face="Courier New"> <span style="COLOR: #000000">;<br /></span> <span style="COLOR: #008080">15</span>聽</font> <font face="Courier New"> <span style="COLOR: #000000">聽聽聽聽}<br /></span> <span style="COLOR: #008080">16</span>聽</font> <font face="Courier New"> <span style="COLOR: #000000">};<br /></span> <span style="COLOR: #008080">17</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">18</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">partial聽specialazation聽for聽T*</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">19</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template聽</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span></font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">20</span>聽<span style="COLOR: #000000">聽聽聽聽struct聽FuncImpl</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">T聽</span><span style="COLOR: #000000">*></span></font> <font face="Courier New"> <span style="COLOR: #000000">聽{<br /></span> <span style="COLOR: #008080">21</span>聽<span style="COLOR: #000000">聽聽聽聽</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">聽</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">聽apply(T聽</span><span style="COLOR: #000000">*</span></font> <font face="Courier New"> <span style="COLOR: #000000">t)聽{<br /></span> <span style="COLOR: #008080">22</span>聽<span style="COLOR: #000000">聽聽聽聽聽聽聽聽</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">聽</span><span style="COLOR: #000000">2</span></font> <font face="Courier New"> <span style="COLOR: #000000">;<br /></span> <span style="COLOR: #008080">23</span>聽</font> <font face="Courier New"> <span style="COLOR: #000000">聽聽聽聽}<br /></span> <span style="COLOR: #008080">24</span>聽</font> <font face="Courier New"> <span style="COLOR: #000000">};<br /></span> <span style="COLOR: #008080">25</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">26</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">users,聽don't聽touch聽this!</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">27</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template聽</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span></font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">28</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">聽func(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">聽T聽</span><span style="COLOR: #000000">&</span></font> <font face="Courier New"> <span style="COLOR: #000000">t)聽{<br /></span> <span style="COLOR: #008080">29</span>聽<span style="COLOR: #000000">聽聽聽聽</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">聽FuncImpl</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">T</span><span style="COLOR: #000000">></span></font> <font face="Courier New"> <span style="COLOR: #000000">::apply(t);<br /></span> <span style="COLOR: #008080">30</span>聽</font> <font face="Courier New"> <span style="COLOR: #000000">}<br /></span> <span style="COLOR: #008080">31</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">32</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">10</span></font> <font face="Courier New"> <span style="COLOR: #000000">,聽r;<br /></span> <span style="COLOR: #008080">33</span>聽<span style="COLOR: #000000">r聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽func(</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">c</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">);聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">r聽=聽0</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">34</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">r聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽func(</span><span style="COLOR: #000000">8</span><span style="COLOR: #000000">);聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">r聽=聽1</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">35</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">r聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽func(</span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">i);聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">r聽=聽2</span></font> </div> <img src ="http://www.shnenglu.com/ant/aggbug/31201.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/ant/" target="_blank">铓傝殎緇堢粨鑰?/a> 2007-08-30 13:55 <a href="http://www.shnenglu.com/ant/archive/2007/08/30/31201.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>浠嶹in32 API灝佽Thread綾籟1]http://www.shnenglu.com/ant/archive/2007/08/30/31190.html铓傝殎緇堢粨鑰?/dc:creator>铓傝殎緇堢粨鑰?/author>Thu, 30 Aug 2007 02:18:00 GMThttp://www.shnenglu.com/ant/archive/2007/08/30/31190.htmlhttp://www.shnenglu.com/ant/comments/31190.htmlhttp://www.shnenglu.com/ant/archive/2007/08/30/31190.html#Feedback7http://www.shnenglu.com/ant/comments/commentRss/31190.htmlhttp://www.shnenglu.com/ant/services/trackbacks/31190.html鍓嶅嚑澶╁湪瀛indows澶氱嚎紼嬬▼搴忚璁★紝鍙戠幇Win32 API鐢ㄨ搗鏉ョ‘瀹炰笉鎬庝箞鏂逛究錛岀壒鍒槸瀵逛簬C++紼嬪簭鍛樸備簬鏄疄鐜頒簡涓涓畝鍗曠殑灝佽錛屾妧鏈惈閲忓綋鐒朵笉楂橈紝涓嶈繃鐢ㄨ搗鏉ヨ繕鏄瘮杈冩柟渚跨殑銆傚鏋滀綘鐔熸?zhèn)塉ava錛屼綘浼?xì)鍙戠幇杩欎釜瀹炵幇鏈夌偣鍍廕ava鐨凾hread錛屽湪Java涓湁涓ょ鏂規(guī)硶鍙互鍒涘緩涓涓猅hread錛?br />
1.浠嶵hread綾葷戶鎵垮茍瀹炵幇run鏂規(guī)硶錛?/p>
1class聽MyThread聽extends 聽Thread聽{
2聽聽聽聽publicvoid
聽run()聽{
3
聽聽聽聽聽聽聽聽...
4
聽聽聽聽}聽
5
};
6

7//寮鍚嚎紼?/span>
8MyThread聽thread聽=new 聽MyThread;
9thread.start();

2.澹版槑涓涓被瀹炵幇Runnable鎺ュ彛騫跺疄鐜皉un鏂規(guī)硶錛?/p>

1class聽MyRunnable聽implements 聽Runnable聽{
2聽聽聽聽publicvoid
聽run()聽{
3
聽聽聽聽聽聽聽聽...
4
聽聽聽聽}
5
};
6

7//聽寮鍚嚎紼嬄?/span>
8MyThread聽thread聽=new聽MyThread(new 聽MyRunnable);
9thread.start();


鍏蜂綋瀹炵幇
Java鐨勫疄鐜版柟寮忚繕綆椾紭闆咃紝鎴戜滑涔熷彲浠ュ湪C++涓ā鎷燂紝鐢變簬綃囧箙鎵闄愶紝鍦ㄨ繖閲屾垜浠彧鍏蟲敞鍩烘湰鍔熻兘鐨勫疄鐜幫細(xì)
thread.h
聽1#ifndef聽THREAD_H
聽2
#define聽THREAD_H
聽3

聽4#include聽<windows.h>
聽5
聽6#define聽CLASS_UNCOPYABLE(classname)聽\
聽7聽聽聽聽private
:聽\
聽8聽聽聽聽classname(const聽classname&
);聽\
聽9聽聽聽聽classname&聽operator=(const聽classname&
);
10

11struct聽Runnable聽{
12聽聽聽聽virtual聽void聽run()聽=0
;
13聽聽聽聽virtual聽~
Runnable()聽{}
14
};
15

16class聽Thread聽:聽public聽Runnable聽{
17
聽聽聽聽CLASS_UNCOPYABLE(Thread)
18public
:
19聽聽聽聽explicit聽Thread(Runnable* target聽=0
);
20聽聽聽聽virtual聽~
Thread();
21聽聽聽聽virtual聽void
聽run()聽{}
22聽聽聽聽void
聽start();
23聽聽聽聽void
聽join();
24private
:
25聽聽聽聽static聽unsigned聽__stdcall聽threadProc(void*
param);
26private
:
27聽聽聽聽Runnable*
_target;
28
聽聽聽聽HANDLE聽_handle;
29
};
30

31#endif/*THREAD_H*/

鍦ㄤ笂闈㈢殑浠g爜涓垜浠厛瀹氫箟涓涓猂unnable綾伙紝騫朵負(fù)Runnable綾繪坊鍔犵函铏氬嚱鏁皉un錛岄渶瑕佺壒鍒敞鎰忕殑鏄疪unnable綾葷殑铏氭瀽鏋勫嚱鏁幫紝浠諱綍鎯寵鎴愪負(fù)鍩虹被鐨刢lass閮藉簲璇ュ0鏄庢瀽鏋勫嚱鏁頒負(fù)virtual銆?br />Thread綾葷洿鎺ョ戶鎵胯嚜Runnable錛孋LASS_UNCOPYABLE(Thread) 榪欎竴琛岀敤瀹忓0鏄嶵hread綾諱笉鍙嫹璐濓紝鍏蜂綋緇嗚妭璇風(fēng)湅 鑰冭檻鐢∕acro鏇挎崲Uncopyable 銆俿tart鎴愬憳鍑芥暟鍜孞ava涓殑涓鏍鳳紝鐢ㄦ潵寮鍚竴涓嚎紼嬶紝join鎴愬憳鍑芥暟鐢ㄦ潵闃誨褰撳墠綰跨▼鐩村埌寮鍚殑綰跨▼鎵ц瀹屾瘯銆倀hreadProc浣滀負(fù)闈欐佹垚鍛樺嚱鏁扮敤鏉ヤ紶閫掔粰_beginthreadex錛岃嚦浜庝負(fù)浠涔堢敤_beginthreadex浠f浛CreateThread璇烽棶Google銆傚垵姝ョ殑瀹炵幇閮芥瘮杈冪畝鍗曪紝鐩存帴璐翠笂浠g爜錛?br />thread.cpp
聽1#include聽"thread.h"
聽2#include聽<process.h>// for聽_beginthreadex
聽3
聽4Thread::Thread(Runnable* target聽/*=聽0*/)
聽5:_target(target
)
聽6,_handle(0
)聽{
聽7

聽8}
聽9

10Thread::~Thread()聽{
11聽聽聽聽if聽(_handle聽!=0
)
12
聽聽聽聽聽聽聽聽CloseHandle(_handle);
13聽聽聽聽if聽(_target聽!=0
)
14
聽聽聽聽聽聽聽聽delete聽_target;
15
}
16

17void聽Thread::start()聽{
18聽聽聽聽if聽(_handle聽!=0
)
19聽聽聽聽聽聽聽聽return
;
20
聽聽聽聽unsigned聽id;
21聽聽聽聽_handle聽=聽reinterpret_cast<HANDLE>
(
22聽聽聽聽聽聽聽聽_beginthreadex(0,聽0,聽threadProc,聽this,聽0,聽&
id)
23
聽聽聽聽聽聽聽聽);
24
}
25

26void聽Thread::join()聽{
27聽聽聽聽if(_handle聽!=0
)聽{
28
聽聽聽聽聽聽聽聽WaitForSingleObject(_handle,聽INFINITE);
29
聽聽聽聽聽聽聽聽CloseHandle(_handle);
30聽聽聽聽聽聽聽聽_handle聽=0
;
31
聽聽聽聽}
32
}
33

34unsigned聽__stdcall聽Thread::threadProc(void* param)聽{
35聽聽聽聽Thread* p聽=聽static_cast<Thread*>
(param);
36聽聽聽聽if聽(p->_target聽!=0
)
37聽聽聽聽聽聽聽聽p->_target->
run();
38聽聽聽聽else

39聽聽聽聽聽聽聽聽p->run();
40聽聽聽聽return0
;
41}

涓嬮潰鏄祴璇曚唬鐮侊細(xì)
test.cpp
聽1#include聽"thread.h"
聽2#include聽<iostream>
聽3
聽4using聽namespace聽std;
聽5

聽6//絎竴縐嶆柟娉曪紝浠嶵hread綾葷戶鎵?/span>
聽7struct聽MyThread聽:聽public聽Thread聽{
聽8聽聽聽聽virtual聽void
聽run()聽{
聽9聽聽聽聽聽聽聽聽for聽(int聽i聽=0;聽i聽<5;聽++
i)聽{
10聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<"MyThread聽Running..."<<聽i聽<<
聽endl;
11聽聽聽聽聽聽聽聽聽聽聽聽Sleep(100
);
12
聽聽聽聽聽聽聽聽}
13
聽聽聽聽}
14
};
15

16//絎簩縐嶆柟娉曪紝鈥滃疄鐜扳漅unnable鎺ュ彛
17struct聽MyRunnable聽:聽public聽Runnable聽{
18聽聽聽聽virtual聽void
聽run()聽{
19聽聽聽聽聽聽聽聽for聽(int聽i聽=0;聽i聽<5;聽++
i)聽{
20聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<"MyRunnable聽Running..."<<聽i聽<<
聽endl;
21聽聽聽聽聽聽聽聽聽聽聽聽Sleep(300
);
22
聽聽聽聽聽聽聽聽}
23
聽聽聽聽}
24
};
25

26int聽main()聽{
27

28聽聽聽聽MyThread聽thread1;
29聽聽聽聽Thread聽thread2(new
聽MyRunnable());
30
聽聽聽聽thread1.start();
31
聽聽聽聽thread2.start();
32
聽聽聽聽thread1.join();
33
聽聽聽聽thread2.join();
34

35聽聽聽聽return0;
36}

鍙兘鐨勮繍琛岀粨鏋滐細(xì)
MyThread Running...0
MyRunnable Running...0
MyThread Running...1
MyThread Running...2
MyRunnable Running...1
MyThread Running...3
MyThread Running...4
MyRunnable Running...2
MyRunnable Running...3
MyRunnable Running...4


濡傛灉浣犵啛鎮(zhèn)塨oost搴擄紝浣犱細(xì)鐭ラ亾boost搴撶殑Thread鏋勯犲嚱鏁板彲浠ユ帴鍙楁櫘閫氬嚱鏁板拰鍑芥暟瀵硅薄浣滀負(fù)鍙傛暟錛屽鏋滀綘瑙夊緱浠嶵hread綾葷戶鎵挎垨鑰呭疄鐜癛unnable鎺ュ彛榪樹笉澶熺畝媧侊紝涓嬩竴綃囦細(xì)鏈変竴涓瘮杈冨ソ鐨勬敼榪涖?

]]>
鑰冭檻鐢∕acro鏇挎崲Uncopyablehttp://www.shnenglu.com/ant/archive/2007/08/27/30908.html铓傝殎緇堢粨鑰?/dc:creator>铓傝殎緇堢粨鑰?/author>Mon, 27 Aug 2007 00:11:00 GMThttp://www.shnenglu.com/ant/archive/2007/08/27/30908.htmlhttp://www.shnenglu.com/ant/comments/30908.htmlhttp://www.shnenglu.com/ant/archive/2007/08/27/30908.html#Feedback2http://www.shnenglu.com/ant/comments/commentRss/30908.htmlhttp://www.shnenglu.com/ant/services/trackbacks/30908.html1. 灝嗚繖涓被鐨刢opy鏋勯犲嚱鏁板拰copy assignment鎿嶄綔絎﹂兘澹版槑涓簆rivate鑰屼笖娌℃湁瀹氫箟銆傜ず渚嬩唬鐮佸涓嬶細(xì)
1class聽SomeClass聽{
2
聽聽聽聽...
3private
:
4聽聽聽聽//鍙湁澹版槑

5聽聽聽聽SomeClass(const聽SomeClass&);
6聽聽聽聽SomeClass&聽operator=(const聽SomeClass&
);
7};

2. 涓撻棬璁捐涓涓負(fù)浜嗛樆姝opying鍔ㄤ綔鑰岃璁$殑base class錛岃繖涓猙ase class鐩稿綋綆鍗曪細(xì)
1Uncopyable聽{
2protected:聽//鍏佽derived瀵硅薄鏋勯犲拰鏋愭瀯

3聽聽聽聽Uncopyable()聽{}
4聽聽聽聽~
Uncopyable()聽{}
5private:聽//闃繪copying

6聽聽聽聽Uncopyable(const聽Uncopyable&);
7聽聽聽聽Uncopyable&聽operator=(const聽Uncopyable&
);
8};

涓轟簡闃繪鏌愪釜瀵硅薄濡係omeClass琚玞opying錛屾垜浠敮涓闇瑕佸仛鐨勫氨鏄戶鎵胯嚜Uncopyable錛?br />
1SomeClass聽:聽private聽Uncopyable聽{
2
聽聽聽聽...
3};

絎?縐嶆柟妗堣矊浼煎緢瀹岀編錛屽湪澶у鏁版儏鍐典笅涔熷伐浣滅殑寰堝ソ錛岀敋鑷砨oost搴撲篃騫挎硾閲囩敤浜?boost搴撲腑鐨勫彨noncopyable)銆傚綋鐒朵簡錛屽洜涓篣ncopyable涓嶅惈浠諱綍鏁版嵁錛岀鍚圗BO(empty base class optimization錛岀┖鐧藉熀綾繪渶浼樺寲)璧勬牸錛屼絾鐢變簬瀹冩繪槸鎵紨base class錛屽洜姝ゆ湁鍙兘瀵艱嚧澶氶噸緇ф壙錛岃孍BO涓鑸彧鍦ㄥ崟緇ф壙涓嬫墠鍙錛岀紪璇戝櫒閫氬父涓嶄細(xì)瀵規(guī)湁澶氫釜base class鐨刣erived classes鏂借EBO銆?br />
濡傛灉浣犱笉鎯沖鑷村閲嶇戶鎵匡紝涓嶇鏄粠鎬ц兘涓婅繕鏄功鍐欎唬鐮佷笂錛屾瘮濡係omeClass緇ф壙鑷煇涓猙ase class錛屼綘鍙兘浼?xì)鍐欏涓嬩唬鐮佸Q?br />
1class聽SomeClass聽:聽public聽BaseClass,聽private聽Uncopyable聽{
2
聽聽聽聽...
3};

榪欒浜哄緢涓嶇埥錛岃鏄綘璺熸垜涓鏍鋒噿錛屼綘鑲畾涔熶笉浼?xì)鎰挎剰閲囩敤绗竴縐嶆柟妗堬紝鍥犱負(fù)閭f剰鍛崇潃浣犱細(xì)鍦ㄦ瘡涓涓笉鎯寵copying鐨刢lass閲岄兘鍔犱笂閭d笁琛屼唬鐮併?br />
濡傛灉鐢∕acro灝卞緢綆鍗曚簡錛屽嵆鐏墊椿鍙堟柟渚匡細(xì)
1#define聽CLASS_UNCOPYABLE(classname)聽\
2聽聽聽聽private
:聽\
3聽聽聽聽classname(const聽classname&
);聽\
4聽聽聽聽classname&聽operator=(const聽classname&);

鐜板湪SomeClass鍙渶鍐欐垚榪欐牱錛?br />
1class聽SomeClass聽{
2
聽聽聽聽CLASS_UNCOPYABLE(SomeClass)
3
聽聽聽聽...
4};

涓嶄粎鐪佷簨鑰屼笖鐏墊椿錛侊紒錛?img src ="http://www.shnenglu.com/ant/aggbug/30908.html" width = "1" height = "1" />

]]>
97超级碰碰碰久久久久| 精品久久久久久国产免费了| 亚洲精品无码久久久久久| 精品熟女少妇AV免费久久| 99re久久精品国产首页2020| 免费一级做a爰片久久毛片潮| 7777精品久久久大香线蕉| 中文字幕一区二区三区久久网站| 亚洲另类欧美综合久久图片区| 国产产无码乱码精品久久鸭| 青青热久久国产久精品 | 久久亚洲综合色一区二区三区| 久久精品国产精品青草| 久久精品无码一区二区WWW| 国产午夜精品理论片久久| 久久人爽人人爽人人片AV| 亚洲午夜精品久久久久久app| 久久精品www| 国内精品久久久久影院日本| 少妇无套内谢久久久久| 久久高潮一级毛片免费| 伊人久久大香线蕉影院95| 丁香狠狠色婷婷久久综合| 无码人妻久久一区二区三区免费| 久久亚洲天堂| 亚洲AⅤ优女AV综合久久久| 99精品久久久久久久婷婷| 99久久精品毛片免费播放| 久久国产精品无码一区二区三区| 久久无码专区国产精品发布 | 亚洲午夜久久久影院伊人| 人妻系列无码专区久久五月天| 久久综合九色综合久99| 久久亚洲综合色一区二区三区| 精品久久久久久亚洲| 高清免费久久午夜精品| 国产精品久久久久影院色| 99久久精品国产免看国产一区| 国产精品久久国产精麻豆99网站| 国产精品久久久久久久久免费| 99久久国产综合精品麻豆|