Usage of CreateEvent and setevent and WaitForSingleObject in threads

Source: Internet
Author: User

Usage of CreateEvent and setevent and WaitForSingleObject in threads

The first introduction of CreateEvent is to create Windows event meaning, the role is mainly used in determining thread exit, Process locking.

CreateEvent

Function Description: Create or open a named or Nameless event object.

There are two states of the event: signal, no signal.

Setevent/resetevent the event to each of these two states is signaled and not signaled.

WaitForSingleObject () Waits until the object specified by the parameter becomes signaled state,

Object can be an event, or it can be another kernel object.

When you create a thread, the thread is actually a loop, not just one run at a time like the one above.

This brings up the problem of finding the right conditions in that loop to get out of the dead loop, so how do you achieve it?

In Windows, there are often events, and there are other ways of doing it.

In this article, we first introduce the way of using events to notify the exit from the thread running function, it is the principle of implementation,

The WaitForSingleObject function is constantly used in that loop to check if the event is satisfied,

If it is satisfied, exit the thread and continue running if it is not satisfied.

When you run a blocking function in a thread, you need to turn the blocking state into a non-blocking state when you exit the threads.

For example, to use a thread to receive network data while using a blocked socket, the socket must be closed before sending an event signal to exit the thread.

Of course, I feel that important applications are also used to lock down the implementation of so-called PV functions.

functions, parameters, etc. are described below.

1.CreateEvent

function function Description: Create or open a named or Nameless event object

Function Prototypes:

// Security Properties  //  Reset mode  / / Initial status  //  object name );

Parameters:

Lpeventattributes:

[Enter] A pointer to the SECURITY_ATTRIBUTES structure to determine whether the returned handle can be inherited by the quilt process.

If lpeventattributes is null, this handle cannot be inherited.

Members in the structure of Windows nt/2000:lpeventattributes Specify a security character for the new event.

If lpeventattributes is null, the event obtains a default security character.

bManualReset:

[input] Specifies whether the event object is created to be manually restored or automatically restored.

If true, you must use the ResetEvent function to manually restore the state of the event to a signal-free state.

If set to False, when an event is released by a waiting thread, the system automatically reverts the event state to a no-signal state.

Binitialstate:

[input] Specifies the initial state of the event object. If true, the initial state is signaled, otherwise there is no signal state.

Lpname:

[Enter] The name of the object that specifies the event, which is a string pointer that ends with 0.

The character format of the name is limited to MAX_PATH. The name is case-sensitive.

If lpname specifies a name that is the same as the name of an existing named event object,

The function will request event_all_access to access the existing object.

At this point, because the bManualReset and binitialstate parameters are already set in the process of creating the event, these two parameters are ignored.

If Lpeventattributes is a parameter that is not NULL, it determines whether the handle can be inherited, but its security descriptor members are ignored.

If lpname is null, an unnamed event object is created.

If the lpname is the same as an existing signal, mutex, wait timer, job, or file-mapped object name, the function will fail.

The Error_invalid_handle is returned in the GetLastError function. This behavior is caused by the fact that these objects share a single namespace.

Terminal Services (Terminal service): The name can be prefixed with "global\" or "local\",

This makes it possible to explicitly create an object in a global or transactional namespace. Other parts of the name except the backslash (\),

You can use any character. For more information, refer to kernel Object Name Spaces.

return value:

If the function call succeeds, the function returns a handle to the event object.

If the named object is already created before the function call, the function returns the handle to the event object that exists.

and returns error_already_exists in the GetLastError function.

If the function fails, the function returns a value of NULL, and if you need to get detailed error information, you need to call GetLastError.

Note:

Call the handle returned by the CreateEvent function that has the Event_all_access permission to access the new event object.

It can also be used in any function that has this event object handle.

During the call, all threads can specify the event object handle in a wait function.

A single-object wait function returns when the state of the specified object is set to signaled state.

For a multi-object wait function, you can specify that any or all of the specified objects be set to a signaled state.

When the wait function returns, the waiting thread is freed to continue running.

The initial state is set in the Binitialstate parameter.

Use the SetEvent function to set the state of the event object to a signaled state.

Use the ResetEvent function to set the state of the event object to a signal-free state.

When the state of a manually restored event object is set to a signaled state, the state of the object remains signaled,

Until you explicitly call the ResetEvent function to place it in an unsigned state.

When an object of an event is set to a signaled state, any number of threads waiting for the thread, and then the waiting line, are freed.

When the state of an automatically restored event object is set to signaled state, the state of the object remains signaled,

Until a waiting thread is released, the system automatically resets the function to an unsigned state.

If no wait thread is waiting, the state of the event object remains signaled.

Multiple processes can hold multiple handles to the same event object, and you can use this object to synchronize between processes.

The following object sharing mechanisms are possible:

In the CreateEvent function, when the Lpeventattributes parameter specifies that the handle can be inherited,
The handle to the event object inherited by the child process created by the CreateProcess function.

A process can specify the event object handle in the DuplicateHandle function,
To get a copy of the handle, this handle can be used by other processes.

A process can specify a name in the OpenEvent or CreateEvent function,
Thus obtaining a well-known event object handle.

Use the CloseHandle function to close the handle. When the process stops, the system automatically closes the handle.
When the last handle is closed, the event object is destroyed.

Once an event is created, you can use the OpenEvent () API to get its handle, close it with CloseHandle () ,
Use SetEvent () or pulseevent () to set it to a signal, with the resetevent () to make it signal-free,
Use WaitForSingleObject () or waitformultipleobjects () to wait for it to become signaled.

PulseEvent () is a more interesting way to use it, as the name of this API, which makes an event

The state of an object occurs once in a pulse, from no signal to a signal and then to no signal, while the entire operation is atomic.

For an event object that is automatically reset, it frees only the first thread to wait for the event, if any,
For a manually reset event object, it frees all the waiting thread.


2. Usage of WaitForSingleObject

The use of WaitForSingleObject

DWORD WaitForSingleObject (HANDLE hhandle,dword dwmilliseconds);

The parameter hhandle is a handle to an event,

The second parameter, Dwmilliseconds, is the time interval.

If time is signaled, return to WAIT_OBJECT_0,
Returns Wait_timeout if the time exceeds the dwmilliseconds value but the time event or no signal state.

The WaitForSingleObject function is used to detect the signal state of the Hhandle event,
When the execution time of the function exceeds dwmilliseconds, it returns.
However, if the parameter dwmilliseconds is infinite, the function will not return until the corresponding time event becomes signaled.
Otherwise, wait until WaitForSingleObject has returned straight before executing the code behind.

Usage of CreateEvent and setevent and WaitForSingleObject in threads

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.