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

            這個kisser不太冷
            posts - 100, comments - 102, trackbacks - 0, articles - 0

            Win32 Thread API學習

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

            第一個 API :創建一個線程

            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: 創建一個線程

            #include <windows.h>

            ?

            DWORD WINAPI threadFunc (LPVOIDpArg)

            {

            ?????? return 0;

            }

            ?

            main ()

            {?????

            ?????? HANDLE hThread;

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

            ?????? return 0;

            }

            ?

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

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



            第二個
            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: 四個線程一起運行

            #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;

            }

            久久最新精品国产| 国产精品一区二区久久| 久久久精品久久久久影院| 久久久久久久久久久| 国产精品久久99| 日韩欧美亚洲综合久久| 一本一道久久精品综合| 精品久久久久久久国产潘金莲| 久久天天躁狠狠躁夜夜avapp| 久久AⅤ人妻少妇嫩草影院| 久久人人爽人人爽人人AV东京热 | 久久免费小视频| 亚洲日本久久久午夜精品| 精品午夜久久福利大片| 日韩欧美亚洲综合久久 | 伊人久久大香线蕉精品不卡| 久久精品国产99久久久| 亚洲欧美精品一区久久中文字幕| 久久99国产精一区二区三区| 国产美女亚洲精品久久久综合| 99热精品久久只有精品| 国产精品天天影视久久综合网| 伊人久久久AV老熟妇色| 久久久久亚洲AV成人网人人网站 | 欧美久久综合性欧美| 国产91色综合久久免费分享| 久久久久成人精品无码中文字幕 | 久久国产视屏| 久久精品无码一区二区日韩AV| 99久久夜色精品国产网站| 久久99国产精品尤物| 日韩人妻无码精品久久免费一| 久久国产免费直播| 天天爽天天狠久久久综合麻豆| 77777亚洲午夜久久多喷| 狠狠色噜噜色狠狠狠综合久久| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 久久久久国产一区二区| 欧美精品福利视频一区二区三区久久久精品 | 久久国产亚洲高清观看| 97久久精品无码一区二区|