• <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久久无毒不卡| www.久久热.com| 99久久精品九九亚洲精品| 久久久亚洲AV波多野结衣| 久久精品国产亚洲AV不卡| 国产91色综合久久免费分享| 日韩欧美亚洲综合久久影院Ds | 久久91这里精品国产2020| 国产精品久久久久aaaa| 久久人妻少妇嫩草AV无码专区| 久久久国产乱子伦精品作者| 亚洲第一永久AV网站久久精品男人的天堂AV| 中文字幕久久精品| 久久精品国产精品青草app| 久久久久久综合一区中文字幕| 99热成人精品免费久久| 深夜久久AAAAA级毛片免费看| 热99RE久久精品这里都是精品免费| 免费无码国产欧美久久18| 久久久久人妻一区二区三区vr| 亚洲国产精品久久久久| 人妻无码久久精品| 99久久精品免费看国产| 久久人人爽人人爽人人片av麻烦| 久久久久高潮毛片免费全部播放 | 久久精品国产精品青草| 欧美粉嫩小泬久久久久久久 | 麻豆AV一区二区三区久久 | 久久亚洲AV成人无码电影| 日本三级久久网| MM131亚洲国产美女久久| 青青青青久久精品国产h久久精品五福影院1421 | 97久久精品人人做人人爽| 久久久久久精品免费免费自慰| 久久久青草久久久青草| 久久亚洲国产成人影院| 国产精品99久久久久久董美香| 国产成人精品综合久久久| 久久精品国产国产精品四凭 | 久久男人AV资源网站| 狠狠色丁香久久综合五月|