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

            }

            久久青青草原亚洲av无码| 99精品久久久久久久婷婷 | 韩国三级中文字幕hd久久精品 | 国内精品久久久久久久久| 久久精品国产第一区二区三区| 久久这里有精品视频| 久久97久久97精品免视看| 久久国产成人亚洲精品影院| 久久99久久成人免费播放| 久久黄视频| 中文精品99久久国产| 国内精品人妻无码久久久影院导航| 麻豆久久| 久久久久精品国产亚洲AV无码| 久久久久亚洲AV成人网人人网站| 久久国产劲爆AV内射—百度| 99精品久久久久久久婷婷| 久久久国产乱子伦精品作者| 一本色道久久HEZYO无码| 亚洲AV无码1区2区久久| 久久亚洲精品成人av无码网站 | 久久夜色精品国产噜噜亚洲AV| 五月丁香综合激情六月久久| 久久99亚洲网美利坚合众国| 久久国产精品-久久精品| 99久久精品国产毛片| 久久久久久亚洲精品不卡| 国产A级毛片久久久精品毛片| 久久久久久国产精品无码超碰| yellow中文字幕久久网| 亚洲精品无码久久不卡| 久久香蕉超碰97国产精品| 久久久久人妻一区二区三区vr| 伊人色综合久久天天| 久久精品视频一| 1000部精品久久久久久久久| 99久久国产免费福利| 无遮挡粉嫩小泬久久久久久久| 一本伊大人香蕉久久网手机| 无码八A片人妻少妇久久| 国产成人无码久久久精品一|