• <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>
            We do not always find visible happiness in proportion to visible virtue

            夢幻白樺林

            SHARE

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              14 Posts :: 58 Stories :: 62 Comments :: 0 Trackbacks

            公告

            常用鏈接

            留言簿(5)

            搜索

            •  

            最新隨筆

            最新評論

            閱讀排行榜

            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


            小熊 2007-03-30 14:58 發表評論

            文章來源:http://m.shnenglu.com/jacky2019/archive/2007/03/30/20938.html
            posted on 2007-03-31 23:03 colys 閱讀(150) 評論(0)  編輯 收藏 引用

            久久久亚洲精品蜜桃臀| | 夜夜亚洲天天久久| 久久亚洲美女精品国产精品| 中文国产成人精品久久亚洲精品AⅤ无码精品| 久久99精品国产麻豆宅宅| 精品国产乱码久久久久久1区2区| 久久久国产99久久国产一| 色婷婷狠狠久久综合五月| 久久乐国产精品亚洲综合| 要久久爱在线免费观看| 久久夜色精品国产亚洲| 中文字幕无码精品亚洲资源网久久 | 久久精品国产亚洲5555| 久久久久国产一区二区三区| 久久久久99精品成人片三人毛片| 国产综合免费精品久久久| 久久性生大片免费观看性| 一本综合久久国产二区| 一本色综合久久| 国产精品美女久久久久久2018| 久久99精品国产自在现线小黄鸭| 久久精品国产亚洲网站| 久久伊人影视| 人妻久久久一区二区三区| 欧美综合天天夜夜久久| 尹人香蕉久久99天天拍| 97精品伊人久久大香线蕉app| 国产精品gz久久久| 久久午夜夜伦鲁鲁片免费无码影视 | 精品免费tv久久久久久久| 久久久久国产一区二区三区| 久久精品卫校国产小美女| 亚洲国产精品人久久| 久久无码高潮喷水| 国内精品免费久久影院| 麻豆成人久久精品二区三区免费| 草草久久久无码国产专区| 亚洲AV日韩精品久久久久| 欧洲国产伦久久久久久久| 高清免费久久午夜精品|