• <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++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            C++多線程(一)

            WIN 多線程API

            一 簡單實例
            比較簡單的代碼,創建10個線程,其中使第4個線程在一創建就掛起,等到其他的線程執行的差不多的時候再使第4個線程恢復執行。
            #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的說明
            CreateThread() 調用成功返回句柄和一個id。
            CloseHandle()  關閉一個打開的對象句柄,該對象句柄可以是線程句柄,也可以是進程、信號量等其他內核對象的句柄.

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

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

            ExitThread是推薦使用的結束一個線程的方法,當調用該函數時,當前線程的棧被釋放,然后線程終止,相對于TerminateThread函數來說,這樣做能夠更好地完成附加在該線程上的DLL的清除工作. 但是ExitThread()會導致線程在清處構造器/自動變量之前就終止,所以我們最好不要顯示的調用ExitThread()。

            posted on 2007-07-24 18:16 夢在天涯 閱讀(13726) 評論(5)  編輯 收藏 引用 所屬分類: CPlusPlus

            評論

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

            不大喜歡這樣的的API調用,希望學習Boost.Thread或者ACE的線程封裝。  回復  更多評論   

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

            BOOST的線程庫不是挺好嗎,自己造輪子嗎  回復  更多評論   

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

            支持樓主  回復  更多評論   

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

            Multithreading Applications in Win32  回復  更多評論   

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

            為啥只創建和結束線程,沒有對線程如何進行分工完成各自任務一起編進來呢,這樣就完美了,對我的用處就非常大了,不過還是有學習價值的,呵呵。  回復  更多評論   

            公告

            EMail:itech001#126.com

            導航

            統計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804303
            • 排名 - 5

            最新評論

            閱讀排行榜

            精品乱码久久久久久久| 性做久久久久久久久久久| 久久er99热精品一区二区| 狠狠综合久久综合88亚洲 | 久久精品国产亚洲AV影院| 久久久久久久久久久久久久| 欧美黑人激情性久久| 99久久精品免费看国产一区二区三区| 久久久久久久久久久免费精品| 亚洲欧美成人综合久久久| 91久久九九无码成人网站| 久久亚洲中文字幕精品一区| 97久久超碰国产精品旧版| 欧美一区二区久久精品| 93精91精品国产综合久久香蕉| 思思久久99热只有频精品66| 久久中文字幕一区二区| 久久亚洲日韩看片无码| 久久久久亚洲AV无码专区桃色| 久久久久亚洲精品天堂| 久久婷婷国产剧情内射白浆| 久久精品国产一区二区三区不卡 | 国产精品久久久久天天影视| 国产99久久久国产精品小说| 久久成人精品| 久久久久久免费一区二区三区| 国产A级毛片久久久精品毛片| 亚洲精品高清一二区久久| 999久久久免费国产精品播放| 久久精品夜夜夜夜夜久久| 无码超乳爆乳中文字幕久久| 天天影视色香欲综合久久| 午夜视频久久久久一区| 亚州日韩精品专区久久久| 少妇久久久久久被弄到高潮| 开心久久婷婷综合中文字幕| 老司机午夜网站国内精品久久久久久久久| 久久精品嫩草影院| 久久国产综合精品五月天| 久久夜色精品国产亚洲av| 久久久久亚洲精品天堂久久久久久 |