• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

            C++多線程(一)

            WIN 多線程API

            一 簡(jiǎn)單實(shí)例
            比較簡(jiǎn)單的代碼,創(chuàng)建10個(gè)線程,其中使第4個(gè)線程在一創(chuàng)建就掛起,等到其他的線程執(zhí)行的差不多的時(shí)候再使第4個(gè)線程恢復(fù)執(zhí)行。
            #include <stdio.h> 
            #include 
            <stdlib.h> 
            #include 
            <windows.h> 

            #define THREAD_NUM 10

            DWORD WINAPI PrintThreads (LPVOID);

            int main () 

                HANDLE hThread[THREAD_NUM]; 
                DWORD dwThreadID[THREAD_NUM]; 

                
            for (int i=0; i<THREAD_NUM; ++i) 
                

                    
            int isStartImmediate = 0;
                    
            if(3 == i)
                        isStartImmediate 
            = CREATE_SUSPENDED;
                    hThread[i]
            =CreateThread(NULL,                // security attributes that should be applied to the new thread, 
                                                                                             
            // this is for NT. Use NULL to get the default security attributes. Use NULL for win95 
                                            0,                                            // default size of 1MB can be passed by passing zero. 
                                            PrintThreads,                     // function name:address of the function where the new thread starts.
                                            (LPVOID)i,                         // parameter(void pointer): pointer to the 32 bit parameter that will be passed into the thread
                                            isStartImmediate,             // flags to control the creation of the thread. Passing zero starts the thread immediately. 
                                                                                      
            // Passing CREATE_SUSPENDED suspends the thread until the ResumeThread( ) function is called.
                                            &dwThreadID[i]            // pointer to a 32-bit variable that receives the thread identifier.
                                            ); 
                    
            if (hThread[i])
                    

                        printf (
            "Thread launched successfully\n");                
                    }
                     
                }
             
                printf(
            "Start sleep 100, and let other thread excute\n");
                Sleep (
            100);    

                printf(
            "Start sleep 100, and thread 3 excute\n");
                ResumeThread(hThread[
            3]);
                
                Sleep(
            100);

                
            for(int i = 0; i<THREAD_NUM; ++i)
                
            {
                    
            if (hThread[i])
                    
            {            
                        CloseHandle(hThread[i]);    
            // You need to use this to release kernel objects when you are done using them. 
                                                    
            // If a process exits without closing the thread handle, 
                                                    
            // the operating system drops the reference counts for those objects. 
                                                    
            // But if a process frequently creates threads without closing the handles, 
                                                    
            // there could be hundreds of thread kernel objects lying around and these resource leaks can have a big hit on performance.
                    }
             
                }

                
            return (0); 
            }
             

            //function PrintThreads 
            DWORD WINAPI PrintThreads (LPVOID num)
            {
                
            for (int i=0; i<10; i++
                    printf (
            "Thread Number is %d%d%d\n", num,num,num); 
                
            return 0;
            }


            二 其他基本API的說(shuō)明
            CreateThread() 調(diào)用成功返回句柄和一個(gè)id。
            CloseHandle()  關(guān)閉一個(gè)打開(kāi)的對(duì)象句柄,該對(duì)象句柄可以是線程句柄,也可以是進(jìn)程、信號(hào)量等其他內(nèi)核對(duì)象的句柄.

            SuspendThread(HANDLE) 允許開(kāi)發(fā)人員將HANDLE指定的線程掛起,如果要掛起的線程占有共享資源,則可能導(dǎo)致死鎖。
            ResumeThread(HANDLE)  恢復(fù)指定的線程。

            TerminateThread() 立即終止線程的工作,不做任何清理工作。
            ExitThread() 線程函數(shù)返回時(shí)回調(diào)用次函數(shù),所以一般我們不去顯示的調(diào)用。

            ExitThread是推薦使用的結(jié)束一個(gè)線程的方法,當(dāng)調(diào)用該函數(shù)時(shí),當(dāng)前線程的棧被釋放,然后線程終止,相對(duì)于TerminateThread函數(shù)來(lái)說(shuō),這樣做能夠更好地完成附加在該線程上的DLL的清除工作. 但是ExitThread()會(huì)導(dǎo)致線程在清處構(gòu)造器/自動(dòng)變量之前就終止,所以我們最好不要顯示的調(diào)用ExitThread()。

            posted on 2007-07-24 18:16 夢(mèng)在天涯 閱讀(13727) 評(píng)論(5)  編輯 收藏 引用 所屬分類(lèi): CPlusPlus

            評(píng)論

            # re: C++多線程(一) 2007-07-24 19:18 pass86

            不大喜歡這樣的的API調(diào)用,希望學(xué)習(xí)Boost.Thread或者ACE的線程封裝。  回復(fù)  更多評(píng)論   

            # re: C++多線程(一) 2007-07-24 21:41 ano

            BOOST的線程庫(kù)不是挺好嗎,自己造輪子嗎  回復(fù)  更多評(píng)論   

            # re: C++多線程(一) 2007-07-25 16:24 黃大仙

            支持樓主  回復(fù)  更多評(píng)論   

            # re: C++多線程(一) 2008-07-09 23:03 honeymorning

            Multithreading Applications in Win32  回復(fù)  更多評(píng)論   

            # re: C++多線程(一) 2009-04-17 20:52 好開(kāi)心

            為啥只創(chuàng)建和結(jié)束線程,沒(méi)有對(duì)線程如何進(jìn)行分工完成各自任務(wù)一起編進(jìn)來(lái)呢,這樣就完美了,對(duì)我的用處就非常大了,不過(guò)還是有學(xué)習(xí)價(jià)值的,呵呵。  回復(fù)  更多評(píng)論   

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評(píng)論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類(lèi)

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804603
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            一级a性色生活片久久无少妇一级婬片免费放 | 久久久久久综合一区中文字幕 | 亚洲国产高清精品线久久| 久久最新精品国产| 国产精品美女久久久久网| 无码人妻久久一区二区三区免费丨 | 青青热久久国产久精品| 亚洲狠狠综合久久| 久久夜色tv网站| 国产AV影片久久久久久| 久久九九亚洲精品| 国产成人久久精品麻豆一区| 亚洲欧美精品伊人久久| 久久99国产一区二区三区| 久久久久久国产精品免费免费| 久久久久亚洲AV成人网人人网站 | 亚洲综合熟女久久久30p| 亚洲综合伊人久久综合| 久久亚洲精品成人AV| 99久久成人国产精品免费 | 久久无码AV中文出轨人妻| 四虎国产精品成人免费久久| 国产成人综合久久精品红| 日韩久久久久久中文人妻| 97久久精品午夜一区二区| 91久久精品电影| 亚洲国产精品一区二区三区久久 | 99久久亚洲综合精品成人| 91久久国产视频| 久久久久久无码国产精品中文字幕 | 国产精品九九九久久九九| 成人精品一区二区久久| 一个色综合久久| 国产午夜久久影院| 三级片免费观看久久| 久久亚洲AV成人出白浆无码国产| 国产精品一久久香蕉产线看 | 久久精品免费一区二区| 国产成人精品久久一区二区三区| 久久久久婷婷| 999久久久无码国产精品|