zoukankan      html  css  js  c++  java
  • 利用事件对象实现线程同步

    不废话,上代码

    #include<windows.h>
    #include<iostream>
    using namespace std;

    DWORD WINAPI fun1proc(LPVOID lpParameter);
    DWORD WINAPI fun2proc(LPVOID lpParameter);
    int index=0;
    int tickets=100;
    HANDLE g_hEvent;

    void main()
    {
    HANDLE hThread1;
    HANDLE hThread2;

    g_hEvent=CreateEvent(NULL,FALSE,FALSE,"tickets"); //创建事件对象


    //保证应用程序只有一个实例运行
    if(g_hEvent)
    {
       if(ERROR_ALREADY_EXISTS==GetLastError())
       {
        cout<<"only one instance can run!"<<endl;
        int a;
        scanf("%d",&a);
        return ;

       }
    }

        SetEvent(g_hEvent);

    hThread1=CreateThread(NULL,0,fun1proc,NULL,0,NULL);
    hThread2=CreateThread(NULL,0,fun2proc,NULL,0,NULL);
    CloseHandle(hThread1);
    CloseHandle(hThread2);
      
    Sleep(4000);
    CloseHandle(g_hEvent);
    }

    DWORD WINAPI fun1proc(LPVOID lpParameter)
    {
    while(1)
    {
       WaitForSingleObject(g_hEvent,INFINITE);
       if(tickets>0)
       {
        Sleep(1);
        cout<<"thread1 sell ticket"<<tickets--<<endl;
        SetEvent(g_hEvent); //一个线程函数运行完毕后使事件对象处于有信号状态。
       }
       else
        break;     
    }
    return 0;
    }

    DWORD WINAPI fun2proc(LPVOID lpParameter)
    {
    while(1)
    {
       WaitForSingleObject(g_hEvent,INFINITE);
       ResetEvent(g_hEvent);
       if(tickets>0)
       {
        Sleep(1);
        cout<<"thread2 sell ticket"<<tickets--<<endl;
        SetEvent(g_hEvent);  
       }
       else
        break;

    }
    return 0;
    }

    小解释:

    CreateEventA(

        __in_opt LPSECURITY_ATTRIBUTES lpEventAttributes,

        __in     BOOL bManualReset,

        __in     BOOL bInitialState,

        __in_opt LPCSTR lpName

    );

    #define CreateEvent CreateEventA

    在程序中可以通过CreateEvent函数创建或打开一个命名的或匿名的事件对象,该函数的原型如上。该函数的四个参数下解释

    Parameters

    lpEventAttributes

    A pointer to a SECURITY_ATTRIBUTES structure. If this parameter is NULL, the handle cannot be inherited by child processes.

    The lpSecurityDescriptor member of the structure specifies a security descriptor for the new event. If lpEventAttributes is NULL, the event gets a default security descriptor. The ACLs in the default security descriptor for an event come from the primary or impersonation token of the creator.

    bManualReset

    If this parameter is TRUE, the function creates a manual-reset event object, which requires the use of the ResetEvent function to set the event state to nonsignaled. If this parameter is FALSE, the function creates an auto-reset event object, and system automatically resets the event state to nonsignaled after a single waiting thread has been released.

    bInitialState

    If this parameter is TRUE, the initial state of the event object is signaled; otherwise, it is nonsignaled.

    lpName

    The name of the event object. The name is limited to MAX_PATH characters. Name comparison is case sensitive.

    If lpName is NULL, the event object is created without a name.

    If lpName matches the name of an existing semaphore, mutex, waitable timer, job, or file-mapping object, the function fails and the GetLastError function returns ERROR_INVALID_HANDLE. This occurs because these objects share the same name space.

    Return Value

    If the function succeeds, the return value is a handle to the event object. If the named event object existed before the function call, the function returns a handle to the existing object and GetLastError returns ERROR_ALREADY_EXISTS.

    If the function fails, the return value is NULL. To get extended error information, call GetLastError.

  • 相关阅读:
    【转】Reactor与Proactor两种模式区别
    [转] 比较清楚的阻塞与非阻塞和同步与异步
    一眨眼已做开发十年
    【转】Linux CentOS内核编译:下载CentOS源码、编译2.6.32-220的错误(apic.c:819 error 'numi_watchdog' undeclared)
    [转] Makefile经典教程(掌握这些足够)
    [转]centos 下 autoconf版本升级
    centos安装CODEBLOCKS
    【转】linux 编译安装nginx,配置自启动脚本
    Install Qt creator
    LeetCode 983. Minimum Cost For Tickets
  • 原文地址:https://www.cnblogs.com/yuzhould/p/4455030.html
Copyright © 2011-2022 走看看