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

            }

            国产V亚洲V天堂无码久久久| 伊人色综合久久| 久久国产欧美日韩精品| 精品久久香蕉国产线看观看亚洲| 国产L精品国产亚洲区久久| 2021久久精品免费观看| 国产精品久久免费| 99精品久久精品一区二区| 9191精品国产免费久久| 人妻精品久久无码区| 久久精品亚洲乱码伦伦中文| 国产韩国精品一区二区三区久久| 性做久久久久久免费观看| 国产精品欧美久久久天天影视| 午夜精品久久久内射近拍高清| 国产午夜久久影院| 久久久久99精品成人片试看 | 久久精品国产清自在天天线| 日韩精品久久无码人妻中文字幕| 久久人人爽人爽人人爽av| 久久精品国产免费| 国产精品视频久久久| 一本色道久久综合亚洲精品| 久久人人爽人爽人人爽av| 亚洲国产精品久久久久婷婷老年 | 99久久夜色精品国产网站| 一本久久知道综合久久| 一级做a爰片久久毛片看看| 品成人欧美大片久久国产欧美| 久久久中文字幕| 欧美激情精品久久久久| 国产精品视频久久| 99久久综合狠狠综合久久| 久久AⅤ人妻少妇嫩草影院| 91麻精品国产91久久久久 | 国产91久久精品一区二区| 精品国产福利久久久| 99热都是精品久久久久久| 久久国产热这里只有精品| 亚洲人成网站999久久久综合| 亚洲国产日韩欧美久久|