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

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

            如下代碼所示:

            CRITICAL_SECTION g_cs;

            EnterCriticalSection(&g_cs);

            //add your critical code here

            ....

            LeaveCriticalSection(&g_cs);

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

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

            // stdafx.h : 標準系統包含文件的包含文件,

            // 或是經常使用但不常更改的

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

            #pragma once

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

            #include <stdio.h>

            #include <tchar.h>

            #include <iostream>

             

            // critical_section.cpp : 定義控制臺應用程序的入口點。

            //

             

            #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的實例。

            //EnterCriticalSection(&g_cs);

            //LeaveCriticalSection(&g_cs);

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

            BOOL WINAPI TryEnterCriticalSection(

              __inout  LPCRITICAL_SECTION lpCriticalSection

            );

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

            posted on 2008-10-30 23:10 zengfancy 閱讀(517) 評論(0)  編輯 收藏 引用
             
            欧洲精品久久久av无码电影| 久久精品一区二区国产| 国产ww久久久久久久久久| 久久er国产精品免费观看2| 国产免费久久精品99久久| 综合久久精品色| 国产国产成人精品久久| 久久综合九色欧美综合狠狠| 久久婷婷五月综合97色一本一本 | 婷婷久久综合九色综合98| 成人综合伊人五月婷久久| 久久无码一区二区三区少妇| 麻豆AV一区二区三区久久| 一级做a爱片久久毛片| 国产精品99久久久精品无码| 久久99精品国产麻豆婷婷| 亚洲精品乱码久久久久久自慰| 国产精品欧美久久久久无广告 | 国产精品免费福利久久| 免费一级欧美大片久久网| 久久无码av三级| 国产亚洲精久久久久久无码77777| 日本免费一区二区久久人人澡| 色播久久人人爽人人爽人人片aV | 久久夜色精品国产噜噜亚洲AV | 香港aa三级久久三级老师2021国产三级精品三级在 | 2022年国产精品久久久久| 久久久久久久久久久| 一本色综合久久| 久久久久亚洲AV成人网人人软件 | 青青久久精品国产免费看| 亚洲精品国产成人99久久| 久久亚洲精品视频| 香蕉久久夜色精品国产小说| 青青青国产成人久久111网站| 精品久久8x国产免费观看| 亚洲AV无码久久| 久久影院综合精品| 97精品久久天干天天天按摩| 国产精品久久久久无码av| 成人亚洲欧美久久久久|