Multi-threaded series five event kernel objects

Source: Internet
Author: User

The event Kernel Object in all kernel objects is the simplest one. It includes a count and two boolean values. A boolean value indicates whether an event is manually reset or automatically reset, and a Boolean value indicates whether the event is currently triggered.

When a manual reset event is triggered, all threads waiting for the event can change to the scheduling status. When an automatic reset event is triggered, only one thread waiting for the event will change to the scheduling status.

The function prototype for creating an event is as follows:

HANDLE WINAPI CreateEvent(  __in_opt  LPSECURITY_ATTRIBUTES lpEventAttributes,  __in      BOOL bManualReset,  __in      BOOL bInitialState,  __in_opt  LPCTSTR lpName);

The first parameter is the security descriptor. If it is set to null, the default security descriptor is used, and the sub-process cannot inherit the returned handle.

The second parameter "true" indicates manual resetting, and "false" indicates automatic resetting.

The third parameter "false" indicates that the event is not triggered, and "true" indicates that the event is triggered.

The fourth parameter specifies a name for the event. If yes, the function will request the event_all_access permission of the event. In this case, other parameters will be ignored. (This method can be used when the program runs only one instance. If an event exists, it exits)

Setevent: Set the event to the trigger status, and resetevent to the trigger status.

The handle must be closed through closehandle.

Waitforsingleoeject and waitformultipleobjects have a side effect on the automatic reset event. After the event is successfully reset to the untriggered status.

 

We can use two sample programs to feel the convenience of the event.

Now three customers have gone to the smoke hotel, and they have three unblocked cigarettes. The boss has gone to get cigarettes, and these three customers are waiting in front of the counter (waiting for the event ), the boss took the cigarettes (the incident turned into a trigger), paid the bills, took the cigarettes, and left.

In the first example, the subroutine uses a manual reset event. After the event is triggered, three customers can leave at the same time.

# Include <windows. h> # include <iostream> using namespace STD; handle hevent; // global event DWORD winapi customerone (lpvoid LP) {cout <"Customer 1: Boss, 8 mg hongshuang Xi "<Endl; waitforsingleobject (hevent, infinite); cout <" Customer 1: This kind of red and double happiness is refreshing. Thank you. "<Endl; return 0;} DWORD winapi customertwo (lpvoid LP) {cout <" 14 14 blocks "<Endl; waitforsingleobject (hevent, infinite ); cout <"Customer 2: I like this cigarette. Thank you, boss. "<Endl; return 0;} DWORD winapi customerthree (lpvoid LP) {cout <" Customer 3: I want to pack 8 mg Zhongnanhai "<Endl; waitforsingleobject (hevent, infinite); cout <"Customer 3: the average person is not used to this, but I have been using this. "<Endl; return 0;} int main (INT argc, char * argv []) {hevent = createevent (null, true, false, null ); handle hthread [3]; hthread [0] = createthread (null, 0, customerone, null); hthread [1] = createthread (null, 0, customertwo, null); hthread [2] = createthread (null, 0, customerthree, 1000, null); sleep (); cout <"BOSS: The cigarettes you want. "<Endl; setevent (hevent); waitformultipleobjects (3, hthread, true, infinite); cout <Endl <" All threads exit "<Endl; closehandle (hevent); For (INT I = 0; I <3; ++ I) closehandle (hthread [I]); Return 0 ;}

In the second example, an automatic reset event is used, and three customers leave one after another. (The side effects of waiting for success, so the three threads cannot be executed at the same time. Each thread must reset the event to the trigger state after it waits for a success, so that another thread can be executed .)

# Include <windows. h> # include <iostream> using namespace STD; handle hevent; // global event DWORD winapi customerone (lpvoid LP) {cout <"Customer 1: Boss, 8 mg hongshuang Xi "<Endl; waitforsingleobject (hevent, infinite); cout <" Customer 1: This kind of red and double happiness is refreshing. Thank you. "<Endl; setevent (hevent); cout <" Customer 1 left. "<Endl; return 0;} DWORD winapi customertwo (lpvoid LP) {cout <" 14 14 blocks "<Endl; waitforsingleobject (hevent, infinite ); cout <"Customer 2: I like this cigarette. Thank you, boss. "<Endl; setevent (hevent); cout <" Customer 2 left. "<Endl; return 0;} DWORD winapi customerthree (lpvoid LP) {cout <" Customer 3: I want to pack 8 mg Zhongnanhai "<Endl; waitforsingleobject (hevent, infinite); cout <"Customer 3: the average person is not used to this, but I have been using this. "<Endl; setevent (hevent); cout <" Customer 3 left. "<Endl; return 0;} int main (INT argc, char * argv []) {hevent = createevent (null, false, false, null ); handle hthread [3]; hthread [0] = createthread (null, 0, customerone, null); hthread [1] = createthread (null, 0, customertwo, null); hthread [2] = createthread (null, 0, customerthree, 1000, null); sleep (); cout <"BOSS: The cigarettes you want. "<Endl; setevent (hevent); waitformultipleobjects (3, hthread, true, infinite); cout <Endl <" All threads exit "<Endl; closehandle (hevent); For (INT I = 0; I <3; ++ I) closehandle (hthread [I]); Return 0 ;}

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.