Thread synchronization--kernel object implements thread synchronization--Event kernel Object

Source: Internet
Author: User

1 Event Kernel Object2 3 There are two different types of event type objects, manual reset and automatic reset4 Manual Reset: When a manual reset object is triggered, all threads waiting for the object become available for dispatch. 5 Automatic Reset: When an automatic reset object is triggered, only one thread waiting for the event becomes scheduled6 7 here is a function to create an event kernel object:8 HANDLE CreateEvent (9 lpsecurity_attributes Lpeventattributes,Ten BOOL bManualReset, One BOOL Binitialstate, A pctstr lpname); -  - the first to fourth parameter is similar to creating other kernel objects, presumably familiar.  the It 's not a referral here.  - The second parameter, bManualReset, is a bool value that tells the system whether to reset the object manually (TRUE) or to reset the object automatically (FALSE). - The third parameter, Binitialstate, is whether the event is initialized to the trigger state (TRUE), or the state is not triggered (FALSE) -  + BOOL SetEvent (HANDLE hevent); - The function turns the event into a trigger state +  A BOOL resetevent (HANDLE hevent); at The function changes the event to a non-triggering state -  -  - How to use: - 1) - //defining an Event object 1 in HANDLE g_hevent; -  to 2) + //Create a manually reset event object and put the event in the trigger State -G_hevent =CreateEvent (null,false,true,null); the  * 3) $ //called in the threadPanax Notoginseng DWORD WINAPI threadfunone (PVOID pvparam) - { the WaitForSingleObject (g_hevent,infinite); +g_x++; A  the return 0; + } -  $#include"windows.h" $#include"iostream" - using namespacestd; - LongG_x =0 ; the  - //defining an Event object 1Wuyi HANDLE g_hevent;  the  - //defining thread Functions 1 Wu DWORD WINAPI threadfunone (PVOID pvparam); -  About //Defining thread Functions 2 $ DWORD WINAPI threadfuntwo (PVOID pvparam); -  - intMain () - { A  +     //Create a manually reset event object and put the event in the trigger State theG_hevent =CreateEvent (null,true,true,null); -  $     //set event to not trigger state the     //resetevent (g_hevent); the  the     //set event to trigger State the     //SetEvent (g_hevent); -  in     //Create thread 1 theHANDLE Hthreadone = CreateThread (NULL,0, Threadfunone,0,0, NULL); the CloseHandle (hthreadone); About  the     //Create thread 2 theHANDLE hthreadtwo = CreateThread (NULL,0, Threadfuntwo,0,0, NULL); the CloseHandle (hthreadtwo); +  -     //let the main thread hang first to make sure that the other threads perform the completion theSleep ( +); Bayicout<<g_x<<Endl; the     return 0 ; the } -  - DWORD WINAPI threadfunone (PVOID pvparam) the { the WaitForSingleObject (g_hevent,infinite); theg_x++; the  -     return 0; the } the  the DWORD WINAPI threadfuntwo (PVOID pvparam)94 { theSleep ( $); the WaitForSingleObject (g_hevent,infinite); theg_x++; 98  About     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.