• 转:CreateEvent函数在多线程中使用及实例


    测试1:

    bManualReset:TRUE
    bInitialState:TRUE

    CreateEvent(NULL, TRUE, TRUE, NULL); //使用手动重置为无信号状态,初始化时有信号状态

    example.cpp

    #include "iostream"
    #include "windows.h"
    using namespace std;
    
    DWORD WINAPI ThreadProc1(LPVOID lpParam);
    DWORD WINAPI ThreadProc2(LPVOID lpParam);
    HANDLE hEvent = NULL;
    HANDLE hThread1 = NULL;
    HANDLE hThread2 = NULL;
    int main(int argc, char *args[])
    {
        <span style="color:#ff0000;">hEvent = CreateEvent(NULL, TRUE, TRUE, NULL)</span>; //使用手动重置为无信号状态,初始化时有信号状态
        //hEvent = CreateEvent(NULL, FALSE, TRUE, NULL); //当一个等待线程被释放时,自动重置为无信号状态,初始是有信号状态
        //if (SetEvent(hEvent))
        //{
        //    cout << "setEvent 成功" <<endl;
        //}
        hThread1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc1, NULL, 0,NULL);
        Sleep(200);
        hThread2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc2, NULL, 0,NULL);
        Sleep(200);
        if ( NULL == hThread1)
        {
            cout <<"create thread fail!";
        }
        //DWORD dCount = ResumeThread(hThread);
        //cout << LOWORD(dCount) << endl;
        return 0;
    }
    DWORD WINAPI ThreadProc1(LPVOID lpParam)
    {
        cout <<"in thread1@!"<<endl;
        
        DWORD dReturn = WaitForSingleObject(hEvent,INFINITE);
        
        if ( WAIT_OBJECT_0 == dReturn)
        {
            cout <<" thread1 signaled ! "<<endl;
        }
        cout <<"in thread1 --signal"<<endl;
        
        //SetEvent(hEvent);
        return 0;
    }
    DWORD WINAPI ThreadProc2(LPVOID lpParam)
    {
        cout <<"in thread2@!"<<endl;
        
        DWORD dReturn = WaitForSingleObject(hEvent,INFINITE);
        
        if ( WAIT_OBJECT_0 == dReturn)
        {
            cout <<"thread2 signaled ! "<<endl;
        }
        cout <<"in thread2--signal"<<endl;
        
        return 0;
    }
  • 相关阅读:
    [POI2014]KUR-Couriers
    MySQL有哪些索引
    索引的设计原则
    explain参数之extra
    explain参数之type
    explain参数之select_type
    如何查询最后一行的记录
    为什么MySQL自增id不连续?
    MySQL字符集
    MySQL有哪些优化策略?
  • 原文地址:https://www.cnblogs.com/killer-xc/p/6612220.html
Copyright © 2020-2023  润新知