Windows Multithreading (vii) Event events

Source: Internet
Author: User
Tags mutex

Previously said mutex mutex and critical segment criticalsection can not implement thread synchronization, can only achieve mutual exclusion, next we use the time event to achieve thread synchronization, the event is a kernel object.

I. Correlation function description (i) Create Event 1. Function prototypes
HANDLE WINAPI CreateEventW(                _In_opt_ LPSECURITY_ATTRIBUTES lpEventAttributes,                _In_ BOOL bManualReset,                _In_ BOOL bInitialState,                _In_opt_ LPCWSTR lpName                );
2. Parameter description
    • The first parameter represents the security control, which is typically passed directly to null.

    • The second parameter determines whether the event is set manually or automatically, passing in true to indicate a manual setting, and passing in false to indicate an automatic position. If set to Automatic, ResetEvent () is called automatically after the event is called WaitForSingleObject () to cause the event to become non-triggering.

    • The third parameter represents the initial state of the event, and an incoming Trur indicates that it has been triggered.

    • The fourth parameter represents the name of the event, and passing NULL indicates an anonymous event.

(ii) Open Event 1. Function prototypes
HANDLE WINAPI OpenEventW(                _In_ DWORD dwDesiredAccess,                _In_ BOOL bInheritHandle,                _In_ LPCWSTR lpName                );
2. Parameter description
    • The first parameter represents the access permission, which is typically passed to event_all_access for an event.

    • The second parameter represents the inheritance of the event handle, which is generally passed in true.

    • The third parameter represents a name, and each thread in a different process can make sure that they access the same event by name.

(iii) Trigger event 1. Function prototypes
BOOL WINAPI SetEvent(              _In_ HANDLE hEvent            );
2. Parameter description
    • Function Description: After each trigger, there must be one or more threads in the waiting state to become a scheduled state.

    • Hevent the handle of the event to be triggered (kernel object)

(iv), set the event to end trigger 1. Function prototypes
BOOL WINAPI ResetEvent(              _In_ HANDLE hEvent            );
2. Parameter description
    • Hevent the handle of the event to be triggered (kernel object)
Second, examples

We can not achieve thread synchronization with the critical segment and the mutex, in the previous program, we could implement mutually exclusive access to global resources, that is, each thread to the global resource plus one, now use the event can be time thread synchronization, that is, to implement each thread sequentially to the global resource plus one, the code is as follows:

//Use events for thread synchronization#include <iostream>#include <windows.h>using namespaceStd Critical_section G_csvar; HANDLE g_event;Const intThread_num =Ten;intG_num =0;D word WINAPI Func (LPVOID);intMain () {g_event = CreateEvent (NULL,false,false, NULL);//Initialize event is not triggered stateInitializeCriticalSection (&g_csvar);    DWORD Threadid[thread_num]; HANDLE Handle[thread_num];inti =0; while(I < Thread_num) {Handle[i] = CreateThread (NULL,0, Func, &i,0, &threadid[i]); WaitForSingleObject (G_event, INFINITE);//Wait for event to be triggeredi++;    } waitformultipleobjects (Thread_num, handle, TRUE, INFINITE);    CloseHandle (g_event); DeleteCriticalSection (&g_csvar);return 0;} DWORD WINAPI Func (LPVOID p) {intNthreadnum = * (int*) p;    EnterCriticalSection (&g_csvar); cout <<"Thread number is:"<< Nthreadnum <<"to the global resource G_num plus 1, now to the global resource G_num value is:"<< ++g_num << Endl;    LeaveCriticalSection (&g_csvar); SetEvent (g_event);//Trigger event    return 0;}

The results of the operation are as follows:

Windows Multithreading (vii) Event events

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.