• <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學習之Critical Sections

            Posted on 2007-03-30 14:58 kk 閱讀(559) 評論(0)  編輯 收藏 引用 所屬分類: IT

            Critical Sections 并不是核心對象。因此沒有 handle 這樣的東西。它和核心對象不同,它存在于進程的內存空間中。你不需要使用想“ Create ”這樣的 API 函數獲得一個 critical section handle 。你應該做的是將一個類型為 CRITICAL_SECTION 的局部變量初始化,方法是調用 InitializeCriticalSection():

            The InitializeCriticalSection function initializes a critical section object.

            VOID InitializeCriticalSection(

            ??? LPCRITICAL_SECTION lpCriticalSection ?????????????? // address of critical section object?

            ?? );?????????

            ?Parameters

            lpCriticalSection

            Points to the critical section object.

            Return Values

            This function does not return a value.


            當你用畢
            critical section 時,你必須調用 DeleteCriticalSection() 清除它。這個函數并沒有“釋放對象”的意義在里頭,不要把它和 C++ delete 運算符混淆了。

            The DeleteCriticalSection function releases all resources used by an unowned critical section object.

            VOID DeleteCriticalSection(

            ??? LPCRITICAL_SECTION lpCriticalSection ?????????????? // pointer to critical section object?

            ?? );?????????

            Parameters

            lpCriticalSection

            Points to the critical section object.

            Return Values

            This function does not return a value.


            一旦
            critical section 被初始化,每一個線程就可以進入其中——只要他通過了 EnterCriticalSection() 這一關:

            The EnterCriticalSection function waits for ownership of the specified critical section object. The function returns when the calling thread is granted ownership.

            VOID EnterCriticalSection(

            ??? LPCRITICAL_SECTION lpCriticalSection ?????????????? // pointer to critical section object?

            ?? );?????????

            Parameters

            lpCriticalSection

            Points to the critical section object.

            Return Values

            This function does not return a value.


            當線程準備好離開
            critical section 的時候,他必須調用 LeaveCriticalSection():

            The LeaveCriticalSection function releases ownership of the specified critical section object.

            VOID LeaveCriticalSection(

            ??? LPCRITICAL_SECTION lpCriticalSection ?????????????? // address of critical section object?

            ?? );?????????

            Parameters

            lpCriticalSection

            Points to the critical section object.

            Return Values

            This function does not return a value.


            下面是一個很簡單的使用例子,只是為了演示如何使用,沒有其它目的:

            #include <stdio.h>

            #include <windows.h>

            const int numThreads = 3;

            //?¨ò?ò???critical section±?á?

            CRITICAL_SECTION gCS;

            ?

            DWORD WINAPI helloFunc(LPVOID pArg)

            {

            ?????? //??è?critical section

            ?????? EnterCriticalSection(&gCS);

            ??????

            ?????? int num = (int) pArg;

            ?????? printf("Hello Thread %d\n", num);

            ?

            ?????? //??3ìó?±?£???μ?μ÷ó?′?oˉê?à??acritical section?££¨Enter**ó?Leave**μ?ê?á?òa????£?

            ?????? LeaveCriticalSection(&gCS);

            ?????? return 0;

            }

            ?

            int main()

            {?????

            ?????? //3?ê??ˉcritical section

            ?????? InitializeCriticalSection(&gCS);

            ?????? HANDLE hThread[numThreads];

            ?????? DWORD begin = GetTickCount();

            ?

            ?????? for (int i = 0; i < numThreads; i++)

            ?????? {

            ????????????? hThread[i] = CreateThread(NULL, 0, helloFunc, (LPVOID)i, 0, NULL);

            ?????? }

            ?

            ?????? WaitForMultipleObjects(numThreads, hThread, TRUE, INFINITE);

            ?????? DWORD elapse = GetTickCount() - begin;

            ?????? printf("elapse time: %d\n", elapse);

            ?

            ?????? //?ùó???3ì?′??íê±?oó£?deleteμ?critical section

            ?????? DeleteCriticalSection(&gCS);

            ?????? return 0;

            }

            ?

            ?

            Google
            97精品伊人久久大香线蕉app | 天天综合久久一二三区| 久久国产视频99电影| 大美女久久久久久j久久| 亚洲午夜久久影院| 狠狠综合久久综合中文88| 久久天天躁狠狠躁夜夜2020| 三级片免费观看久久| 国产A级毛片久久久精品毛片| 亚洲精品乱码久久久久久| …久久精品99久久香蕉国产| 日韩精品国产自在久久现线拍| 欧美久久久久久午夜精品| 久久久久久国产精品无码超碰| 久久精品视频网| 久久久www免费人成精品| 久久综合丁香激情久久| 漂亮人妻被黑人久久精品| 久久精品无码专区免费| 1000部精品久久久久久久久| 欧美精品国产综合久久| 国产精品激情综合久久| 久久久久亚洲av无码专区喷水 | 色综合久久综合中文综合网| 成人午夜精品久久久久久久小说| 久久久噜噜噜久久熟女AA片| 免费无码国产欧美久久18| 久久久久亚洲AV成人网人人软件| 国产综合久久久久| 亚洲国产精品一区二区久久hs| 三级韩国一区久久二区综合| 久久久久国产一级毛片高清板 | 婷婷国产天堂久久综合五月| 狠狠综合久久综合中文88| 国产精品久久久天天影视香蕉| 亚洲国产成人久久精品影视| .精品久久久麻豆国产精品| 国内精品久久久久影院一蜜桃| 精品久久久久香蕉网| 国产产无码乱码精品久久鸭| 久久国产欧美日韩精品|