• <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>

            Kisser Leon

            這個(gè)kisser不太冷
            posts - 100, comments - 102, trackbacks - 0, articles - 0

            Win32 Thread API學(xué)習(xí)

            Posted on 2007-03-18 17:40 kk 閱讀(2788) 評論(0)  編輯 收藏 引用 所屬分類: IT

            第一個(gè) API :創(chuàng)建一個(gè)線程

            CreateThread()

            The CreateThread function creates a thread to execute within the address space of the calling process.

            HANDLE CreateThread(

            ??? LPSECURITY_ATTRIBUTES lpThreadAttributes,??????????????? // pointer to thread security attributes?

            ??? DWORD dwStackSize,??????????????? // initial thread stack size, in bytes

            ??? LPTHREAD_START_ROUTINE lpStartAddress,? // pointer to thread function

            ??? LPVOID lpParameter,// argument for new thread

            ??? DWORD dwCreationFlags,??????? // creation flags

            ??? LPDWORD lpThreadId ?????????????? // pointer to returned thread identifier

            ?? );

            Return Values

            If the function succeeds, the return value is a handle to the new thread.

            If the function fails, the return value is NULL. To get extended error information, call etLastError.

            Example1: 創(chuàng)建一個(gè)線程

            #include <windows.h>

            ?

            DWORD WINAPI threadFunc (LPVOIDpArg)

            {

            ?????? return 0;

            }

            ?

            main ()

            {?????

            ?????? HANDLE hThread;

            ?????? hThread = CreateThread (NULL, 0, threadFunc, NULL, 0, NULL );

            ?????? return 0;

            }

            ?

            上面這段代碼有一個(gè)小問題,那就是 threadFunc 有可能不會被執(zhí)行到就結(jié)束了!要保證 threadFunc 肯定被執(zhí)行(也就是等線程退出),在 CreateThread 后加一行代碼:

            ??????????????? WaitForMultipleObjects (1, &hThread, TRUE, INFINITE);



            第二個(gè)
            API :等待線程返回

            WaitForMultipleObjects

            The WaitForMultipleObjects function returns when one of the following occurs:

            Either any one or all of the specified objects are in the signaled state.

            The time-out interval elapses.

            DWORD WaitForMultipleObjects(

            ??? DWORD nCount,????????? // number of handles in the object handle array

            ??? CONST HANDLE *lpHandles,?? // pointer to the object-handle array

            ??? BOOL bWaitAll,???????????? // wait flag

            ??? DWORD dwMilliseconds ????????? // time-out interval in milliseconds

            ?? );

            If dwMilliseconds is INFINITE, the function's time-out interval never elapses.

            Example2: 四個(gè)線程一起運(yùn)行

            #include <Windows.h>

            #include <stdio.h>

            const int numThreads = 4;

            ?

            DWORD WINAPI threadFunc(LPVOIDpArg)

            {

            ?????? int *p = (int *)pArg;

            ?????? int num = *p;

            ?????? printf("threadFunc...%d...\n", num);

            ?????? return 0;

            }

            ?

            int main()

            {

            ?????? HANDLE hThread[numThreads];

            ?????? int threadNum[numThreads];

            ?????? for(inti=0; i<numThreads; i++)

            ?????? {

            ????????????? threadNum[i] = i;

            ????????????? hThread[i] = CreateThread(NULL, 0, threadFunc, &threadNum[i], 0, NULL);

            ?????? }

            ??????

            ?????? WaitForMultipleObjects(numThreads, hThread, TRUE, INFINITE);

            ?????? return 0;

            }

            久久国产一区二区| 狠狠色丁香婷婷久久综合| 久久777国产线看观看精品| 欧美一区二区精品久久| 久久久WWW成人免费精品| 亚洲欧洲久久久精品| 国产成人久久精品一区二区三区| 日本一区精品久久久久影院| 亚洲人成网站999久久久综合 | 亚洲国产精品无码久久SM| 精品熟女少妇av免费久久| 久久国产一片免费观看| 久久久久久久97| 亚洲欧美精品一区久久中文字幕 | 国产福利电影一区二区三区,免费久久久久久久精 | 亚洲国产精品无码久久一区二区| 亚洲成人精品久久| 久久天堂AV综合合色蜜桃网| 精品久久久久久久久久中文字幕| 热re99久久6国产精品免费| 少妇久久久久久被弄到高潮| 一级做a爰片久久毛片16| 精品久久久久久国产潘金莲 | 久久综合狠狠色综合伊人| 99久久精品国产一区二区| 久久精品国产精品亜洲毛片| 久久综合综合久久97色| 人妻精品久久久久中文字幕69| 久久久精品久久久久影院| 久久www免费人成精品香蕉| 久久中文娱乐网| 99久久免费只有精品国产| 久久精品国产精品青草app| WWW婷婷AV久久久影片| 久久AV高清无码| 97久久天天综合色天天综合色hd| 狠狠色婷婷久久一区二区 | 久久精品国产精品亚洲精品 | 国产精品久久久久AV福利动漫| 国内高清久久久久久| 色欲久久久天天天综合网精品|