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;
}
原文地址:https://www.cnblogs.com/davidsguo008/p/3610860.html