《Windows核心编程》第九章——用内核对象进行线程同步

先举一个有bug的例子:

#include <iostream>
#include <windows.h>
#include <process.h>

using namespace std;
#define MAX_SIZE 0x500

HANDLE g_hSubmit;
HANDLE g_hReturn;
HANDLE g_hStop;
char g_szInput[MAX_SIZE] = {0};



unsigned int _stdcall ThreadServer(void* pParam)
{
    while (TRUE)
    {
        int nWaitRes = WaitForSingleObject(g_hStop, 100);
        if (WAIT_OBJECT_0 == nWaitRes)
            break;
        

        WaitForSingleObject(g_hSubmit, INFINITE);
        printf("Recieve:%s\n", g_szInput);
        SetEvent(g_hReturn);
    }
    SetEvent(g_hStop);
    printf("Set stop\n");
    return 0;
}

void main()
{
    int count = 0;
    g_hSubmit = CreateEvent(NULL, FALSE, FALSE, NULL);
    g_hReturn = CreateEvent(NULL, FALSE, FALSE, NULL);
    g_hStop = CreateEvent(NULL, FALSE, FALSE, NULL);

    HANDLE hTheadServer = (HANDLE)_beginthreadex(NULL, 0, ThreadServer, NULL, 0, NULL);
    while (TRUE)
    {
        count++;
        printf("Input:");
        cin.getline(g_szInput, MAX_SIZE);
        SetEvent(g_hSubmit);
        WaitForSingleObject(g_hReturn, INFINITE);
        if (count == 2){
            Sleep(2000);
            SetEvent(g_hStop);
            
            break;
        }
    }

    HANDLE hHandle[3];
    hHandle[0] = g_hStop;
    hHandle[1] = hTheadServer;
    WaitForMultipleObjects(2, hHandle, TRUE, INFINITE);
    CloseHandle(hTheadServer);
    CloseHandle(g_hReturn);
    CloseHandle(g_hStop);
    CloseHandle(g_hSubmit);
    getchar();

}

起初,我想要设置一个事件——g_hStop来通知线程,使得ThreadServer线程能够被主线程停止,但是这里出现了一个问题,如果我刻意让主线程Sleep2秒再去SetEvent,那么等待g_hStop的wait函数就会超时,从而往下继续执行等待Input,而此时主线程已经退出input循环,那么就会死锁。所以我改为使用全局变量来使得Threadserver线程退出:

#include <iostream>
#include <windows.h>
#include <process.h>

using namespace std;
#define MAX_SIZE 0x500

HANDLE g_hSubmit;
HANDLE g_hReturn;
HANDLE g_hStop;
int g_nCount = 0;
char g_szInput[MAX_SIZE] = {0};



unsigned int _stdcall ThreadServer(void* pParam)
{
    while (TRUE)
    {
        if (g_nCount == 2)
            break;

        WaitForSingleObject(g_hSubmit, INFINITE);
        printf("Recieve:%s\n", g_szInput);
        SetEvent(g_hReturn);
    }
    SetEvent(g_hStop);
    printf("Set stop\n");
    return 0;
}

void main()
{
    g_hSubmit = CreateEvent(NULL, FALSE, FALSE, NULL);
    g_hReturn = CreateEvent(NULL, FALSE, FALSE, NULL);
    g_hStop = CreateEvent(NULL, FALSE, FALSE, NULL);

    HANDLE hTheadServer = (HANDLE)_beginthreadex(NULL, 0, ThreadServer, NULL, 0, NULL);
    while (TRUE)
    {
        g_nCount++;
        printf("Input:");
        cin.getline(g_szInput, MAX_SIZE);
        SetEvent(g_hSubmit);
        WaitForSingleObject(g_hReturn, INFINITE);
        if (g_nCount == 2){
            Sleep(2000);
            SetEvent(g_hStop);
            
            break;
        }
    }

    HANDLE hHandle[3];
    hHandle[0] = g_hStop;
    hHandle[1] = hTheadServer;
    WaitForMultipleObjects(2, hHandle, TRUE, INFINITE);
    CloseHandle(hTheadServer);
    CloseHandle(g_hReturn);
    CloseHandle(g_hStop);
    CloseHandle(g_hSubmit);
    getchar();

}

猜你喜欢

转载自www.cnblogs.com/predator-wang/p/8960622.html