C++互斥器:Mutex


互斥器的功能是,使多個線程和諧工作。同一時間內,只能有一個線程得到互斥對象,並獲得資源操作權限,那么如果同一時間其他線程也想去操作資源,此時就會因為Mutex未處於激發狀態,而無奈的等待…這時候,線程就會進入blocking(阻塞)狀態,直到Mutex讓出來。

總結下Mutex的操作步驟,分為以下幾個功能:

1. 產生一個全局互斥器Mutex(一個Mutex可以看做一個資源,如果要多個資源,則需要創建多個Mutex句柄);

2. 鎖住互斥器Mutex:獲得一個Mutex的擁有權,其他線程只能等待。當需要鎖操作時,如果此時鎖未處於激活狀態,線程就得等待(也就是阻塞狀態/sleep)並每隔一段時間嘗試着再次去占用Mutex,不行就繼續阻塞直到Mutex被讓出;

3. 釋放互斥器Mutex,使得后一個等待的線程能夠擁有它並得以獲得資源;

 

這里需要說明的是,Mutex的擁有權並非屬於那個產生它的線程,而是最后那個獲得它且未釋放的線程。線程擁有了Mutex就好像線程進入了臨界區域一樣。一次只能有一個線程獲得Mutex。

和大部分核心對象一樣,Mutex是通過計數來實現互斥,當Mutex被占用時計數為1,當Mutex未被占用時計數降低為0;

但是,有些情況是殘酷的所以必須避免:在一個程序中,線程絕對不應該在即將結束時還擁有一個Mutex,否則該Mutex將無法被釋放。

 

Win32下Mutex相關接口:

1. 創建一個互斥器,該接口返回一個HANDLE:

HANDLE CreateMutex(LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner, LPCTSTR lpName);

2. 打開一個互斥器,該互斥器已被創建過:

HANDLE OpenMutexW(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCWSTR lpName);

3. 等待一個資源對象:

DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds);

--hHandle:mutex句柄

--dwMilliseconds:INFINITE:表示無限等待,否則可以設置時間(ms)

4. 等待多個資源對象:

DWORD WaitForMultipleObjects(DWORD nCount, CONST HANDLE *lpHandles, BOOL bWaitAll, DWORD dwMilliseconds);

--nCount:句柄的數量

--*lpHandles:句柄數組

--bWaitAll:如果為TRUE則所有對象都空閑則獲得權限,如果為FALSE則只要有一個對象空閑就獲得權限

--dwMilliseconds:INFINITE:表示無限等待,否則可以設置時間(ms)

5. 等待多個資源對象,還可以等待消息:

DWORD MsgWaitForMultipleObjects(DWORD nCount, LPHANDLE pHandles, BOOL fWaitAll, DWORD dwMilliseconds, DWORD dwWakeMask);

6. 釋放互斥器

BOOL ReleaseMutex(HANDLE hMutex);

7. 摧毀互斥器

BOOL CloseHandle(HANDLE hObject);

 

哲學家就餐:

我們知道,哲學家就餐問題就是為了解決所有老頭都拿着一支筷子,但都在等待另外雙筷子進行吃飯的尷尬場面。

比如一共5個哲學家,但也只有5支筷子。如果大家同時抓起一根筷子,很快就進入死鎖場面;但如果不允許老頭子拿起一根筷子,而是只允許一次拿起一雙筷子,此時就可以保證至少2個老頭能夠先吃飯了,即使還留着一根筷子另外3個老頭也只能傻等——這樣就能避免大家都吃不到飯的問題

image

那么如何使用Mutex來對哲學家問題進行解決呢?

#include "stdafx.h"

extern HWND hWndMain;
extern int g_nChopstickUser[];
extern int g_nDinerState[];
extern HANDLE g_hChopstickMutex[];
extern HANDLE g_hPhilosThread[];
extern BOOL g_bWaitMultiple;
extern BOOL g_bFastDining;

/****
 * 線程執行
 **/
DWORD WINAPI DiningThread(LPVOID pVoid)
{
    /****
     * 哲學家編號
     **/
    int nPhilosopher = (int)pVoid;
    /****
     * 左手筷子
     **/
    int nLeftChopstick = nPhilosopher;
    /**** 
     * 右手筷子
     **/
    int nRightChopstick = nLeftChopstick + 1;
    if (nRightChopstick > PHILOSOPERS - 1)
    {
        nRightChopstick = 0;
    }

    /****
     * 左右手資源句柄(只能從這2個資源里去拿)
     * 句柄不會產生副本,永遠指向全局
     **/
    HANDLE hChopstickMutex[2] = {0};
    hChopstickMutex[0] = g_hChopstickMutex[nLeftChopstick];
    hChopstickMutex[1] = g_hChopstickMutex[nRightChopstick];

    /****
     * 隨機時間等待
     **/
    srand((unsigned) time(NULL) + (nPhilosopher + 1));

    /****
     * 哲學家等待
     **/
    Sleep(DINING_DELAY);

    /****
    * 筷子狀態:RESTING; WAITING; EATING
    **/
    while(TRUE)
    {
        if (g_bWaitMultiple)
        {
            /** 2根一起拿 **/
            g_nDinerState[nPhilosopher] = WAITING;
            SendMessage(hWndMain, WM_FORCE_REPAINT, NULL, NULL);
            WaitForMultipleObjects(2,hChopstickMutex, TRUE, INFINITE);
            g_nChopstickUser[nLeftChopstick] = nPhilosopher;
            g_nChopstickUser[nRightChopstick] = nPhilosopher;
        }
        else
        {
            /** 一根根拿 **/
            g_nDinerState[nPhilosopher] = WAITING;
            SendMessage(hWndMain, WM_FORCE_REPAINT, NULL, NULL);
            WaitForSingleObject(hChopstickMutex[0], INFINITE);
            g_nChopstickUser[nLeftChopstick] = nPhilosopher;

            Sleep(DINING_DELAY);

            g_nDinerState[nPhilosopher] = WAITING;
            SendMessage(hWndMain, WM_FORCE_REPAINT, NULL, NULL);
            WaitForSingleObject(hChopstickMutex[1], INFINITE);
            g_nChopstickUser[nRightChopstick] = nPhilosopher;
        }

        /**** 
         * 獲取資源,開始吃吃吃吃.....
         **/
        g_nDinerState[nPhilosopher] = EATING;
        SendMessage(hWndMain, WM_FORCE_REPAINT, NULL, NULL);

        Sleep(DINING_DELAY);

        /****
         * 吃完了? "尼瑪,放下那雙筷子....!"
         **/
        g_nDinerState[nPhilosopher] = RESTING;
        g_nChopstickUser[nLeftChopstick] = UNUSED;
        g_nChopstickUser[nRightChopstick] = UNUSED;
        SendMessage(hWndMain, WM_FORCE_REPAINT, NULL, NULL);
        ReleaseMutex(hChopstickMutex[0]);
        ReleaseMutex(hChopstickMutex[1]);

        Sleep(DINING_DELAY);
    }

    return 0;
}

void Dining()
{
    for (int i = 0; i < PHILOSOPERS; i++)
    {
        g_hPhilosThread[i] = CreateThread(NULL, 0, DiningThread, (LPVOID)i, NULL, NULL);
    }
}

 

#pragma once

#include "resource.h"
#include <math.h>

/****
 * 哲學家的數量
 **/
#define PHILOSOPERS            6

/****
 * 每個弧度角
 **/
#define ANGLE_PER_RADIAN    57.295779513082320876798154814105

/****
 * 狀態
 **/
#define UNUSED                -1
#define RESTING                0
#define WAITING                1
#define    EATING                2

/****
 * MSG:強制重繪
 **/
#define WM_FORCE_REPAINT    WM_APP + 1

/****
 * 哲學家等待時間
 **/
#define DINING_DELAY        g_bFastDining ? (rand() / 25) : ((rand() % 5 + 1) * 1000)

/****
 * 哲學家休息時間
 **/
#define DINING_RESTING        (rand() % 6 + 1) * 1000

/****
 * 哲學家吃飯時間
 **/
#define DINING_EATING        (rand() % 5 + 1) * 1000

/****
 * 框架渲染
 **/
void RenderFrame(HDC hdc);

/****
 * 就餐
 **/
void Dining();

 

// Philosoper Dining.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "mtverify.h"

#define MAX_LOADSTRING 100

/************************************************************************/
/*                            全局變量                                    */
/************************************************************************/
HWND hWndMain;                            /* 窗口句柄 */
RECT rcWinExt = {0, 0, 200, 200};        /* 正方形窗口,長寬 == 200 */
int g_nXExt = 200;
int g_nYExt = 200;
int g_nRadiusTable = g_nXExt / 4;        /* 桌子半徑 == 50,設計上讓桌子在窗口中心 */
POINT g_ptWinOrg = {g_nXExt / 2, g_nYExt / 2};    /* 設置坐標原點在窗口中心) */

int g_nChopstickUser[PHILOSOPERS];        /* 筷子使用者 */
int g_nDinerState[PHILOSOPERS];            /* 盤中餐狀態 */

HANDLE g_hChopstickMutex[PHILOSOPERS];    /* 筷子互斥器 */
HANDLE g_hPhilosThread[PHILOSOPERS];    /* 哲學家線程 */

BOOL g_bWaitMultiple = TRUE;            /* 哲學家問題就必須同時等待 */
BOOL g_bFastDining = FALSE;                /* 演示吃快點還是吃慢點 */

/************************************************************************/

// Global Variables:
HINSTANCE hInst;                                // current instance
TCHAR szTitle[MAX_LOADSTRING];                    // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

     // TODO: Place code here.
    MSG msg;
    HACCEL hAccelTable;

    // Initialize global strings
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadString(hInstance, IDC_PHILOSOPERDINING, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_PHILOSOPERDINING));

    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style            = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra        = 0;
    wcex.cbWndExtra        = 0;
    wcex.hInstance        = hInstance;
    wcex.hIcon            = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_PHILOSOPERDINING));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = MAKEINTRESOURCE(IDC_PHILOSOPERDINING);
    wcex.lpszClassName    = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   /****
    * 這里對話框做小點:
    **/
   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      50, 5, 800, 600, NULL, NULL, hInstance, NULL);

   /****
    * 坑爹,因為這里沒改導致所有畫面無法顯示
    * 更新窗口句柄
    */
   hWndMain = hWnd ;

   if (!hWnd)
   {
      return FALSE;
   }
   
   /****
    * 資源初始化
    **/
    for (int i = 0; i < PHILOSOPERS; i++)
    {
        g_nDinerState[i] = RESTING;        /* 盤中餐一開始都是休息狀態的 */
        g_nChopstickUser[i] = UNUSED;    /* 筷子一開始都是未使用狀態的 */
        g_hChopstickMutex[i] = CreateMutex(NULL, FALSE, NULL);    /* 不指定用戶也無名 */
    }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);
   
   /****
    * 開始就餐
    **/
   Dining();

   return TRUE;
}

void RenderFrame(HDC hdc)
{
    RECT rcClient, rc ;
    HPEN hPenOld ;
    HFONT hFont, hFontOld ;
    int  nRadius ;
    int nPos, nPosNext, x, y, nXPhilosopher, nYPhilosopher ;
    double dAngle, dRadian ;
    TCHAR  szName[16] ;

    GetClientRect(hWndMain, &rcClient) ;

    SelectObject(hdc, GetStockObject(NULL_BRUSH)) ;

    SetMapMode(hdc, MM_ANISOTROPIC) ;

    SetWindowExtEx(hdc, g_nXExt, g_nYExt, NULL) ;
    SetWindowOrgEx(hdc, g_ptWinOrg.x, g_ptWinOrg.y, NULL) ;
    SetViewportExtEx(hdc, rcClient.right, rcClient.bottom, NULL) ;
    SetViewportOrgEx(hdc, rcClient.right / 2, rcClient.bottom / 2, NULL) ;

    hFont = CreateFont(5, 0, 0, 0, FW_HEAVY, FALSE, FALSE, FALSE, GB2312_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FIXED_PITCH | FF_SWISS, TEXT("楷體")) ;
    hFontOld = (HFONT) SelectObject(hdc, hFont) ;

    rc.left  = g_ptWinOrg.x - 10 ; rc.top    = g_ptWinOrg.y - 10 ;
    rc.right = g_ptWinOrg.x + 10 ; rc.bottom = g_ptWinOrg.y + 10 ;
    Ellipse(hdc, rc.left, rc.top, rc.right, rc.bottom) ;
    Ellipse(hdc, rc.left - 40, rc.top - 40, rc.right + 40, rc.bottom + 40) ;
    StringCchPrintf(szName, 16, TEXT("紅燒肉")) ;
    DrawText(hdc, szName, -1, &rc,  DT_CENTER | DT_SINGLELINE | DT_VCENTER) ;

    dAngle = 360.0 / PHILOSOPERS ;
    for (nPos = 0 ; nPos < PHILOSOPERS ; nPos++)
    {
        dRadian = (nPos * dAngle) / ANGLE_PER_RADIAN;
        x = g_ptWinOrg.x + (int)(sin(dRadian) * g_nRadiusTable * 0.5) ;
        y = g_ptWinOrg.y - (int)(cos(dRadian) * g_nRadiusTable * 0.5) ;
        MoveToEx(hdc, x, y, NULL) ;

        x = g_ptWinOrg.x + (int)(sin(dRadian) * g_nRadiusTable * 0.9) ;
        y = g_ptWinOrg.y - (int)(cos(dRadian) * g_nRadiusTable * 0.9) ;
        LineTo(hdc, x, y) ;
    }

    for (nPos = 0 ; nPos < PHILOSOPERS ; nPos++)
    {
        nRadius = (int) (g_nRadiusTable * 0.2) ;
        dRadian = (nPos * dAngle + dAngle / 2) / ANGLE_PER_RADIAN ;
        x = g_ptWinOrg.x + (int)(sin(dRadian) * g_nRadiusTable * 0.6) ;
        y = g_ptWinOrg.y - (int)(cos(dRadian) * g_nRadiusTable * 0.6) ;
        rc.left  = x - nRadius ; rc.top    = y - nRadius ;
        rc.right = x + nRadius ; rc.bottom = y + nRadius ;

        switch (g_nDinerState[nPos])
        {
        case RESTING:
            hPenOld = (HPEN) SelectObject(hdc, CreatePen(PS_SOLID, 1, 0x00FF00)) ;
            SetTextColor(hdc, 0x00FF00) ;
            StringCchPrintf(szName, 16, TEXT("休息")) ;
            break ;

        case WAITING:
            hPenOld = (HPEN) SelectObject(hdc, CreatePen(PS_SOLID, 1, 0x0000FF)) ;
            SetTextColor(hdc, 0x0000FF) ;
            StringCchPrintf(szName, 16, TEXT("想吃肉!")) ;
            break ;

        case EATING:
            hPenOld = (HPEN) SelectObject(hdc, CreatePen(PS_SOLID, 1, 0xE28C12)) ;
            SetTextColor(hdc, 0xE28C12) ;
            StringCchPrintf(szName, 16, TEXT("有肉吃!")) ;
            break ;
        }

        Ellipse(hdc, rc.left, rc.top, rc.right, rc.bottom) ; 
        DrawTextEx(hdc, szName, -1, &rc,  DT_CENTER | DT_SINGLELINE | DT_VCENTER, NULL) ;

        nRadius = (int)(g_nRadiusTable * 0.3) ;
        dRadian = (nPos * dAngle + dAngle / 2) / ANGLE_PER_RADIAN ;
        nXPhilosopher = g_ptWinOrg.x + (int)(sin(dRadian) * g_nRadiusTable * 1.4) ;
        nYPhilosopher = g_ptWinOrg.y - (int)(cos(dRadian) * g_nRadiusTable * 1.4) ;

        rc.left  = nXPhilosopher - nRadius ; rc.top    = nYPhilosopher - nRadius ;
        rc.right = nXPhilosopher + nRadius ; rc.bottom = nYPhilosopher + nRadius ;
        Ellipse(hdc, rc.left, rc.top, rc.right, rc.bottom) ;

        SetTextColor(hdc, 0x0) ;
        StringCchPrintf(szName, 16, TEXT("哲學家 %d"), nPos + 1) ;
        DrawText(hdc, szName, -1, &rc,  DT_CENTER | DT_SINGLELINE | DT_VCENTER) ;

        if (g_nChopstickUser[nPos] == nPos)
        {
            dRadian = (nPos * dAngle) / ANGLE_PER_RADIAN ;
            x = g_ptWinOrg.x + (int)(sin(dRadian) * g_nRadiusTable * 0.9) ;
            y = g_ptWinOrg.y - (int)(cos(dRadian) * g_nRadiusTable * 0.9) ;
            MoveToEx(hdc, x, y, NULL) ;
            LineTo(hdc, nXPhilosopher, nYPhilosopher) ;
        }

        nPosNext = nPos + 1 ;
        if (nPosNext >= PHILOSOPERS)
            nPosNext = 0 ;

        if (g_nChopstickUser[nPosNext] == nPos)
        {
            dRadian = (nPosNext * dAngle) / ANGLE_PER_RADIAN ;
            x = g_ptWinOrg.x + (int)(sin(dRadian) * g_nRadiusTable * 0.9) ;
            y = g_ptWinOrg.y - (int)(cos(dRadian) * g_nRadiusTable * 0.9) ;
            MoveToEx(hdc, x, y, NULL) ;
            LineTo(hdc, nXPhilosopher, nYPhilosopher) ;
        }

        DeleteObject(SelectObject(hdc, hPenOld)) ;
    }

    DeleteObject(SelectObject(hdc, hFontOld)) ;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;

    switch (message)
    {
    case WM_CREATE:
        /****
         * 部分選項置灰
         * waitformultipleobjects,create thread, resume thread.
         **/
        EnableMenuItem(GetMenu(hWnd), IDM_WAITFORMULTIPLEOBJECTS, MF_GRAYED | MF_BYCOMMAND);
        EnableMenuItem(GetMenu(hWnd), IDM_CREATETHREAD, MF_GRAYED | MF_BYCOMMAND);
        EnableMenuItem(GetMenu(hWnd), IDM_RESUMETHREAD, MF_GRAYED | MF_BYCOMMAND);
        break;

    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case IDM_WAITFORMULTIPLEOBJECTS:
            /**** 
             * 點擊:waitformultipleobjects
             **/
            EnableMenuItem(GetMenu(hWnd), IDM_WAITFORMULTIPLEOBJECTS, MF_GRAYED | MF_BYCOMMAND);
            EnableMenuItem(GetMenu(hWnd), IDM_FASTDEADLOCK, MF_ENABLED | MF_BYCOMMAND);
            EnableMenuItem(GetMenu(hWnd), IDM_SLOWDEADLOCK, MF_ENABLED | MF_BYCOMMAND);
            g_bWaitMultiple = TRUE;
            g_bFastDining = FALSE;
            break;
        case IDM_FASTDEADLOCK:
            /**** 
             * 點擊:fast deadlock
             **/
            EnableMenuItem(GetMenu(hWnd), IDM_WAITFORMULTIPLEOBJECTS, MF_ENABLED | MF_BYCOMMAND);
            EnableMenuItem(GetMenu(hWnd), IDM_FASTDEADLOCK, MF_GRAYED | MF_BYCOMMAND);
            EnableMenuItem(GetMenu(hWnd), IDM_SLOWDEADLOCK, MF_ENABLED | MF_BYCOMMAND);
            g_bWaitMultiple = FALSE;
            g_bFastDining = TRUE;
            break;
        case IDM_SLOWDEADLOCK:
            /**** 
             * 點擊:slow deadlock
             **/
            EnableMenuItem(GetMenu(hWnd), IDM_WAITFORMULTIPLEOBJECTS, MF_ENABLED | MF_BYCOMMAND);
            EnableMenuItem(GetMenu(hWnd), IDM_FASTDEADLOCK, MF_ENABLED | MF_BYCOMMAND);
            EnableMenuItem(GetMenu(hWnd), IDM_SLOWDEADLOCK, MF_GRAYED | MF_BYCOMMAND);
            g_bWaitMultiple = FALSE;
            g_bFastDining = FALSE;
            break;
        case IDM_CREATETHREAD:
            /**** 
             * 點擊:create thread
             **/
            EnableMenuItem(GetMenu(hWnd), IDM_CREATETHREAD, MF_GRAYED | MF_BYCOMMAND);
            EnableMenuItem(GetMenu(hWnd), IDM_TERMINATETHREAD, MF_ENABLED | MF_BYCOMMAND);
            EnableMenuItem(GetMenu(hWnd), IDM_SUSPENDTHREAD, MF_ENABLED | MF_BYCOMMAND);
            EnableMenuItem(GetMenu(hWnd), IDM_RESUMETHREAD, MF_GRAYED | MF_BYCOMMAND);
            Dining();
            break;
        case IDM_SUSPENDTHREAD:
            /**** 
             * 點擊:suspend thread
             **/
            EnableMenuItem(GetMenu(hWnd), IDM_SUSPENDTHREAD, MF_GRAYED | MF_BYCOMMAND);
            EnableMenuItem(GetMenu(hWnd), IDM_RESUMETHREAD, MF_ENABLED | MF_BYCOMMAND);
            for (int i = 0; i < PHILOSOPERS; i++)
            {
                SuspendThread(g_hPhilosThread[i]);
            }
            break;
        case IDM_RESUMETHREAD:
            /**** 
             * 點擊:resume thread
             **/
            EnableMenuItem(GetMenu(hWnd), IDM_SUSPENDTHREAD, MF_ENABLED | MF_BYCOMMAND);
            EnableMenuItem(GetMenu(hWnd), IDM_RESUMETHREAD, MF_GRAYED | MF_BYCOMMAND);
            for (int i = 0; i < PHILOSOPERS; i++)
            {
                ResumeThread(g_hPhilosThread[i]);
            }
        case IDM_TERMINATETHREAD:
            /**** 
             * 點擊:terminate thread
             **/
            EnableMenuItem(GetMenu(hWnd), IDM_TERMINATETHREAD, MF_GRAYED | MF_BYCOMMAND);
            EnableMenuItem(GetMenu(hWnd), IDM_CREATETHREAD, MF_ENABLED | MF_BYCOMMAND);
            EnableMenuItem(GetMenu(hWnd), IDM_RESUMETHREAD, MF_GRAYED | MF_BYCOMMAND);
            EnableMenuItem(GetMenu(hWnd), IDM_SUSPENDTHREAD, MF_GRAYED | MF_BYCOMMAND);
            for (int i = 0 ; i < PHILOSOPERS ; i++) 
            {
                TerminateThread(g_hPhilosThread[i], i);
                g_nDinerState[i] = RESTING; 
                g_nChopstickUser[i] = UNUSED;
            }
            /* 重繪 */
            InvalidateRect(hWnd, NULL, TRUE);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }

        /****
         * 窗口菜單修改后刷新
         **/
        DrawMenuBar(hWnd) ;
        break;

    case WM_FORCE_REPAINT:
        /****
         * 自定義刷新畫面
         **/
        {
            MSG msg;
            InvalidateRect(hWnd, NULL, TRUE);
            while (PeekMessage(&msg, hWnd, WM_FORCE_REPAINT, WM_FORCE_REPAINT, PM_REMOVE));
        }
        break;

    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        /****
         *
         **/
        RenderFrame(hdc);
        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        /****
         * 刪除資源
         **/
        for (int i = 0; i < PHILOSOPERS; i++)
        {
            CloseHandle(g_hChopstickMutex[i]);
            TerminateThread(g_hPhilosThread[i], i);
        }
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM