Use event objects for Thread Synchronization

Source: Internet
Author: User
//  The event object also belongs to the kernel object and contains a usage count,  //  A boolean value that indicates whether the event is an automatically reset event or a manually reset event,  //  Another Boolean value used to indicate whether the event is in the notified or not notified status.  //  There are two different types of event objects. One is a manual reset event, and the other is an automatic reset event.  //  When a manually reset event is notified, all threads waiting for the event change to schedulable threads.  //  When an automatically reset event is notified, only one thread in the thread waiting for the event changes to a schedulable thread.  # Include <Windows. h> # Include <Iostream>Using   Namespace  STD; DWORD winapi fun1proc (lpvoid lpparameter); DWORD winapi fun2proc (lpvoid lpparameter );  Int Tickets = 100  ; Handle g_hevent;  Void  Main () {handle hthread1; handle hthread2; g_hevent = Createevent (null, false, true, null ); //  Set to auto reset  //  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  );} DWORD winapi fun1proc (lpvoid lpparameter ){  While  (True) {waitforsingleobject (g_hevent, infinite );  If (Tickets> 0 ) {Sleep (  1  ); Cout < "  Thread1 javasticket:  " <Tickets -- < Endl ;}  Else              Break  ; Setevent (g_hevent );}  Return   0  ;} DWORD winapi fun2proc (lpvoid lpparameter ){  While (True) {waitforsingleobject (g_hevent, infinite );  If (Tickets> 0  ) {Sleep (  1  ); Cout < "  Thread2 javasticket:  " <Tickets -- < Endl ;}  Else              Break  ; Setevent (g_hevent );} 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.