• <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>
            編城浪子的博客
            游戲開發(fā)、圖形引擎

            最近一段時期準(zhǔn)備溫習(xí)多線程同步相關(guān)內(nèi)容,主要包括關(guān)鍵代碼段和各種內(nèi)核同步對象。先學(xué)習(xí)最簡單的同步方法--Critical Section。所謂關(guān)鍵代碼段,是指給一段代碼加上鎖,要執(zhí)行這段代碼,必須拿到一把鑰匙,而且這把鑰匙世界上僅存一把,獨(dú)一無二。

            如下代碼所示:

            CRITICAL_SECTION g_cs;

            EnterCriticalSection(&g_cs);

            //add your critical code here

            ....

            LeaveCriticalSection(&g_cs);

            在執(zhí)行...這段代碼前,必須確保g_cs沒有被其他線程占有,否則線程就在此處掛起。通過這種方式可以保護(hù)某個資源,不被多個線程同時訪問。

            以下代碼演示了兩個線程在使用Critical Section和不使用的條件下的輸出結(jié)果:

            // stdafx.h : 標(biāo)準(zhǔn)系統(tǒng)包含文件的包含文件,

            // 或是經(jīng)常使用但不常更改的

            // 特定于項目的包含文件

            #pragma once

            #define WIN32_LEAN_AND_MEAN                                       // Windows 頭中排除極少使用的資料

            #include <stdio.h>

            #include <tchar.h>

            #include <iostream>

             

            // critical_section.cpp : 定義控制臺應(yīng)用程序的入口點(diǎn)。

            //

             

            #include "stdafx.h"

            #include <Windows.h>

             

            using namespace std;

             

            CRITICAL_SECTION g_cs;

            int g_var = 0;

             

            DWORD WINAPI ThreadProc(LPVOID lpParam)

            {

                                cout << "child thread started!\n";

                                for(int i=0;i<30;i++)

                                {

                                                     //EnterCriticalSection(&g_cs);

                                                     cout << "The " << "child ";

                                                     cout << "thread " << "enter ";

                                                     cout << "the CS!" << "\n";

                                                     g_var++;

                                                     cout << "g_var added by 1: g_var = " << g_var << endl;

                                                     cout << "The " << "child " << "thread " << "leave " << "the CS!" << "\n";

                                                     //LeaveCriticalSection(&g_cs);

                                }

                                cout << "child thread ended!\n";

                                return 0;

            }

             

            int _tmain(int argc, _TCHAR* argv[])

            {

                                cout << "main thread started!\n";

                                InitializeCriticalSection(&g_cs);

                                DWORD tid;

             

                                HANDLE hThread = CreateThread(0, 0, ThreadProc, 0, 0, &tid);

             

                                for(int i=0;i<30;i++)

                                {

                                                     //EnterCriticalSection(&g_cs);

                                                     cout << "The " << "main " << "thread ";

                                                     cout << "enter " << "the CS!" << "\n";

                                                     g_var--;

                                                     cout << "g_var minused by 1: g_var = " << g_var << endl;

                                                     cout << "The " << "main " << "thread ";

                                                     cout << "enter " << "the CS! " << "\n";

                                                     //LeaveCriticalSection(&g_cs);

                                }

             

                                WaitForSingleObject(hThread, INFINITE);

             

                                CloseHandle(hThread);

             

                                DeleteCriticalSection(&g_cs);

             

                                cout << "main thread ended!\n";

                                return 0;

            }

            將如下代碼的注釋去掉即變成了使用Critical Section的實(shí)例。

            //EnterCriticalSection(&g_cs);

            //LeaveCriticalSection(&g_cs);

            EnterCriticalSection()的缺點(diǎn)是如果g_cs仍未被其他線程釋放,就一直在此處掛起,如果不想線程掛起,可以用替代函數(shù):

            BOOL WINAPI TryEnterCriticalSection(

              __inout  LPCRITICAL_SECTION lpCriticalSection

            );

            下一章將講述互斥--Mutex的使用。

            posted on 2008-10-30 23:10 zengfancy 閱讀(516) 評論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


             
            久久久久亚洲精品无码网址| 午夜精品久久久内射近拍高清| 久久狠狠爱亚洲综合影院| 色婷婷噜噜久久国产精品12p| 久久精品桃花综合| 久久r热这里有精品视频| 久久99精品国产99久久6| 人妻无码精品久久亚瑟影视| 国产麻豆精品久久一二三| 久久久久亚洲av成人无码电影| 99久久香蕉国产线看观香| 久久久久久无码Av成人影院| 久久乐国产精品亚洲综合| 久久久无码人妻精品无码| 麻豆久久| 激情久久久久久久久久| 77777亚洲午夜久久多喷| 久久这里的只有是精品23| 国产午夜精品理论片久久| 久久精品国产亚洲77777| 久久综合色老色| 久久国产视频99电影| 久久青草国产精品一区| 久久99国内精品自在现线| 久久精品国产亚洲av麻豆蜜芽| 国产免费久久精品丫丫| 久久精品国产福利国产秒| 久久精品中文騷妇女内射| 精品久久久一二三区| 久久亚洲AV无码西西人体| 国产精品久久99| 久久噜噜电影你懂的| 国产精品久久永久免费| 精品久久久噜噜噜久久久 | 看全色黄大色大片免费久久久 | 亚洲国产成人久久精品影视| 欧美大香线蕉线伊人久久| 性高湖久久久久久久久| 亚洲AV无码一区东京热久久 | 7777久久亚洲中文字幕| 久久久久国产精品熟女影院 |