• <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 閱讀(2788) 評論(0)  編輯 收藏 引用 所屬分類: IT

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

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

            #include <windows.h>

            ?

            DWORD WINAPI threadFunc (LPVOIDpArg)

            {

            ?????? return 0;

            }

            ?

            main ()

            {?????

            ?????? HANDLE hThread;

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

            ?????? return 0;

            }

            ?

            上面這段代碼有一個小問題,那就是 threadFunc 有可能不會被執(zhí)行到就結(jié)束了!要保證 threadFunc 肯定被執(zhí)行(也就是等線程退出),在 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精品久久只有精品| 亚洲国产精品无码久久久秋霞2| 久久99国产精品尤物| 国产精品国色综合久久| 亚洲综合精品香蕉久久网97| 精品无码久久久久久久动漫| 蜜桃麻豆WWW久久囤产精品| 亚洲国产精品无码久久久秋霞2 | 色8久久人人97超碰香蕉987| 精品免费久久久久久久| 日批日出水久久亚洲精品tv| 国产亚洲精品自在久久| 日日狠狠久久偷偷色综合0| 99久久免费国产特黄| 亚洲一级Av无码毛片久久精品| 国产精品99久久99久久久| 亚洲欧洲中文日韩久久AV乱码| 97久久精品人妻人人搡人人玩| 亚洲国产成人久久笫一页| 97久久精品人妻人人搡人人玩| 精品久久久一二三区| 国产成人AV综合久久| 国内精品久久久久久99| 久久热这里只有精品在线观看| 久久99国产精品99久久| 97视频久久久| 久久综合九色综合久99| 99久久亚洲综合精品网站| 国产婷婷成人久久Av免费高清| 四虎国产精品成人免费久久| 久久久久国产精品麻豆AR影院| 99久久免费国产精精品| 99久久精品国产麻豆| 国产V亚洲V天堂无码久久久| 色综合久久久久久久久五月| 一本色综合网久久| 亚洲中文字幕久久精品无码APP | 99久久婷婷国产综合精品草原| 国产精品久久99| 岛国搬运www久久| 久久精品成人影院|