搜索
熱搜: 活動 交友 discuz
查看: 3654|回復: 0
打印 上一主題 下一主題

[教學] C++多執行緒(一)

[複製鏈接]
跳轉到指定樓層
1#
發表於 2007-8-14 04:50:35 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
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
=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// pointer to a 32-bit variable that receives the thread identifier.
                                );
        
if (hThread)
        
{
            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)
        
{            
            CloseHandle(hThread);   
// 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()。
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

本論壇為非營利之網路平台,所有文章內容均為網友自行發表,不代表論壇立場!若涉及侵權、違法等情事,請告知版主處理。


Page Rank Check

廣告刊登  |   交換連結  |   贊助我們  |   服務條款  |   免責聲明  |   客服中心  |   中央分站

手機版|中央論壇

GMT+8, 2024-4-29 15:27 , Processed in 0.018966 second(s), 16 queries .

Powered by Discuz!

© 2005-2015 Copyrights. Set by YIDAS

快速回復 返回頂部 返回列表