Multithreading for thread synchronization--event objects

Source: Internet
Author: User

An event object is a thread synchronization that is implemented in a program with or without a signal state of a kernel object.

1. Manipulating Event objects using API functions

The API function is createevent;

The function prototypes are:

HANDLE CreateEvent (  lpsecurity_attributes lpeventattributes,//SD  BOOL bManualReset,                       //Reset Type  BOOL binitialstate,                      //Initial state  LPCTSTR lpname                           //object name);

The parameter lpeventattributes is a structural body security_attributespointer that represents the security properties of the newly created event object. If set to NULL, the default security attribute is used.

The parameter bmanualreset indicates whether the created event object is manually reset or reset automatically. If this argument is true, the event object created by the program is manually reset, or false to indicate that the created event object is automatically reset.

The parameter binitialstate represents the initial state of the event object. If this argument is true, the event object is signaled at the initial time, otherwise, it is in a non-trusted state.

The parameter npname represents the name of the event object. If this parameter is NULL, it means that the program created an anonymous event object.

Use WaitForSingleObject to actively request event objects.

DWORD WaitForSingleObject (  HANDLE hhandle,        //HANDLE to Object  DWORD dwmilliseconds   //time-out Interval);

The parameter dwmilliseconds represents the time the function will wait on the event object, and if it is infinite, the function waits forever.

Use SetEvent to set the specified event object to a signaled state. Use ResetEvent to set the specified event object to a signal-free state.

BOOL SetEvent (  HANDLE hevent   //HANDLE to event);

BOOL resetevent (  HANDLE hevent   //HANDLE to event);

The following is the code implemented by the C language:

#include <stdio.h> #include <windows.h>dword WINAPI myfun1 (LPVOID lpparameter); Declares the thread function DWORD WINAPI myfun2 (LPVOID lpparameter);                                               HANDLE hevent; Define global variables Heventint a = 0;int main () {HANDLE h1, h2;hevent =:: CreateEvent (NULL, FALSE, FALSE, NULL);:: SetEvent (hevent); h1 =     :: CreateThread (NULL, 0, myfun1, NULL, 0, NULL); Create thread printf ("Thread 1 starts running!\r\n"); H2 =:: CreateThread (NULL, 0, myfun2, NULL, 0, NULL);p rintf ("Thread 2 starts running!\r\n");:: CloseHandle (H1);:: CloseHandle (H2);:: Sleep (10000); return 0;}             DWORD WINAPI myfun1 (LPVOID lpparameter)//Thread function 1{while (1) {:: WaitForSingleObject (Hevent, INFINITE);                                Request Event object:: ResetEvent (hevent);                               Set event object to no signal state if (a <) {A + = 1;::sleep (+);p rintf ("Thread 1 is counting%d\r\n", a);: SetEvent (Hevent); Set event object to signaled state}else{::setevent (hevent); break;}} return 0;} DWORD WINAPI myfun2 (LPVOID lpparameter) {while (1) {:: WaitforsingleoBject (hevent, INFINITE);:: ResetEvent (Hevent), if (a <) {A + = 1;::sleep (+);p rintf ("Thread 2 is counting%d\r\n", a);:: SetEvent (hevent);} Else{::setevent (hevent); break;}} return 0;}

Results:



2. Using the CEvent class for thread synchronization

CEvent function Prototypes:

CEvent (bool Binitiallyown = FALSE, bool bManualReset = False, LPCTSTR lpszName = NULL, lpsecurity_attributes lpsaattribut e = NULL);
The parameter Binitiallyown represents the initialization state of the event object. If true, indicates that the event object is signaled, otherwise there is no signal state and the default is no signal state.

The parameter bManualReset indicates whether the event object is manually reset or reset automatically. If this argument is true, the event object is manually reset, otherwise it is automatically reset.

The parameter bpsname represents the name of the event object. The default is null.

The parameter bpsaattribute represents the security properties of the event object. Typically specified as the default security attribute.


The following is the code implemented by the C language:

#include <stdio.h> #include <afxmt.h>//header file, not Windows.hdword WINAPI m                     YFUN1 (LPVOID lpparameter); Declares the thread function DWORD WINAPI myfun2 (LPVOID lpparameter);                        CEvent Event;int A = 0;int main () {CEvent (false, FALSE, NULL, NULL); HANDLE H1, H2; CreateEvent (null, FALSE, FALSE, NULL), event.      SetEvent (); h1 =:: CreateThread (NULL, 0, myfun1, NULL, 0, NULL); Create thread printf ("Thread 1 starts running!\r\n"); H2 =:: CreateThread (NULL, 0, myfun2, NULL, 0, NULL);p rintf ("Thread 2 starts running!\r\n");:: CloseHandle                                         (H1); Close thread Handle object:: CloseHandle (H2);:: Sleep (10000); return 0;} DWORD WINAPI myfun1 (LPVOID lpparameter)//Thread function 1{while (1) {:: WaitForSingleObject (Event.m_hobject, Infin        ITE); Request Event object. ResetEvent (); if (a < 1000)//Set event object to no signal state {A + = 1;::sleep (+);p rintf ("Thread 1 is Count%d\r\n ", a); event.                                  SetEvent (); Set event object to be signaledState}else{event. SetEvent (); break;}} return 0;} DWORD WINAPI myfun2 (LPVOID lpparameter) {while (1) {:: WaitForSingleObject (Event.m_hobject, INFINITE); event. ResetEvent (), if (a <) {A + = 1;::sleep (+);p rintf ("Thread 2 is counting%d\r\n", a); event. SetEvent ();} Else{event. SetEvent (); break;}} return 0;}

Results:



Multithreading for thread synchronization--event objects

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.