Usage of createevent, setevent, and waitforsingleobject in the thread --- interrupt ist in wince is often used.

Source: Internet
Author: User

Usage of createevent, setevent, and waitforsingleobject in a thread

First, we will introduce how createevent is used to create a Windows event. It is mainly used to determine the thread exit and the thread lock.

Createevent

Function Description: Creates or opens a named or unknown event object.
Event has two states: sending a signal, not sending a signal.
Setevent and resetevent respectively set the event to the sending and non-sending statuses.
Waitforsingleobject () waits until the object specified by the parameter becomes a sending signal. The object can be an event or another kernel object.

When you create a thread, the thread is actually a loop, not running only once as above. This poses a problem. In that endless loop, we need to find the appropriate condition to exit the endless loop. How can we implement it? In Windows, events are often used. Of course, other methods can be used. Here, we will first introduce how to use events to notify the function to exit from the thread running function. The implementation principle is as follows. In that endless loop, the waitforsingleobject function is constantly used to check whether the event meets the requirements, if yes, exit the thread. If not, continue running. When running a blocking function in the thread, you must first change the blocking status to non-blocking when exiting the thread. For example, you can use a thread to receive network data, when using a blocked socket, you must first disable the socket and then send an event signal to exit the thread.

Of course, I feel that important applications are used to lock and implement the so-called PV function.

The following describes function functions and parameters.

1. createevent

 

Function Description: Creates or opens a named or unknown event object.

Function prototype:

Handle createevent (

Lpsecurity_attributes lpeventattributes, // Security Attribute

Bool bmanualreset, // reset mode

Bool binitialstate, // initial state

Lptstr lpname // Object Name

);

Parameters:

Lpeventattributes:

[Input] 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.

Windows NT/2000: the members in the lpeventattributes structure specify a security token for the new event. If lpeventattributes is null, the event will get a default security token.

Bmanualreset:

[Input] specify whether to manually restore or automatically restore the event object. If it is true, you must use the resetevent function to manually restore the event state to the stateless state. If this parameter is set to false, when an event is released by a waiting thread, the system automatically restores the event status to the stateless state.

Binitialstate:

[Input] specifies the initial status of the event object. If this parameter is set to true, the initial state is a signal state; otherwise, the initial state is a signal state.

Lpname:

[Input] The name of the specified event object. It 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 the name specified by lpname is the same as the name of an existing named event object, the function requests event_all_access to access the existing object. At this time, because the bmanualreset and binitialstate parameters have been set in the event creation process, these two parameters will be 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 unknown event object is created.

If the lpname is the same as an existing signal, mutex, wait timer, job, or file ing object name, the function will fail and error_invalid_handle will be returned in the getlasterror function. This is because these objects share the same namespace.

Terminal Services: you can add a prefix of "global/" or "Local/" to the name to create an object in a global or transaction namespace. In addition to the backslash (/), other parts of the name can use any character. For more information, see Kernel Object Name spaces.

Windows 2000: in Windows 2000, no terminal service is running, and the prefix "Global/" and "Local/" will be ignored. In addition to the backslash (/), other parts of the name can use any character.

Windows NT 4.0 and earlier versions, Windows 95/98: Except for the backslash (/), the name can contain any character.

Return Value:

If the function is successfully called, the function returns the handle of the event object. If the named object has been created before the function is called, the function returns the handle of the existing event object and returns error_already_exists in the getlasterror function.

If the function fails, the return value is null. To obtain detailed error information, call getlasterror.

Note:

Call the handle returned by the createevent function. The handle has the event_all_access permission to access the new event object. It can also be used in any function with the event object handle.

During the call process, all threads can specify the event object handle in a waiting function. When the status of the specified object is set to a signal state, a single object waits for the function to return.

For the multi-object wait function, you can specify any or all specified objects to be set to a signal state. When the function is returned, the waiting thread is released to continue running.

The initial status is set in the binitialstate parameter. Use the setevent function to set the event object status to a signal state. Use the resetevent function to set the event object state to the non-signal state.

When the status of a manually restored event object is set to a signal state, the state of the object remains a signal State until it is explicitly called by the resetevent function.

When the event object is set to a signal state, any number of waiting threads and threads that start to wait will be released.

When the status of an automatically restored event object is set to a signal state, the state of the object remains a signal State until a waiting thread is released; the system automatically sets this function to the unsigned state. If no waiting thread is waiting, the status of the event object remains signaled.

Multiple processes can hold multiple handles of the same event object. You can use this object to synchronize processes. The following object sharing mechanism is feasible:

· In the createevent function, when the lpeventattributes parameter specifies that the handle can be inherited, the event object handle inherited by the child process created through the CreateProcess function.

· A process can specify the event object handle in the duplicatehandle function to obtain a copy handle, which can be used by other processes.

· A process can specify a name in the openevent or createevent function to obtain a famous event object handle.

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

Environment:

Windows NT/2000: Version 3.1 or later is required

Windows 95/98: Windows 95 or later

Header file: It is defined in WINBASE. h; it must contain windows. h.

Import to database: user32.lib

UNICODE: in Windows NT/2000, it is executed in Unicode and ANSI.

After an event is created, you can use the openevent () API to obtain its handle, and use closehandle ()

To close it, use setevent () or pulseevent () to set it to make it signal, use resetevent ()

To make it non-signal, use waitforsingleobject () or waitformultipleobjects () to wait

It becomes a signal.

Pulseevent () is an interesting method to use. Just like the name of this API, it makes an event

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

For the event object that is automatically reset, it only releases the first thread that waits for the event (if any), and

Manually reset the event object, which releases all waiting threads.

2. waitforsingleobject usage

Waitforsingleobject usage

DWORD waitforsingleobject (

Handle hhandle,

DWORD dwmilliseconds

);

The hhandle parameter is the handle of an event, and the second parameter dwmilliseconds is the time interval. If the time is in the signal state, wait_object_0 is returned. If the time exceeds the dwmilliseconds value but the time event is still in the non-signal state, wait_timeout is returned.

Hhandle can be the handle of the following objects:

Change Notification

Console input

Event

Job

Memory resource notification

Mutex

Process

Semaphore

Thread

Waitable Timer

The waitforsingleobject function is used to detect the signal status of the hhandle event. When the function execution time exceeds dwmilliseconds, it returns. However, if the dwmilliseconds parameter is infinite, the function returns only when the corresponding time event changes to a signal state, otherwise, wait until waitforsingleobject returns the Code directly. Here is an example:

First create a global event object g_event:

Cevent g_event;

In the program, you can call cevent: setevent to set the event to a signal state.

Below is a thread function mythreadpro ()

Uint cflushdlg: mythreadproc (lpvoid pparam)

{

Waitforsingleobject (g_event, infinite );

For (;;)

{

.............

}

Return 0;

}

In this thread function, the following for loop is executed only when g_event is set to a signal state. Because g_event is a global variable, we can use g_event in other threads. setevent controls this thread.

Another method is to use the waitforsingleobject function to execute a thread function at intervals.

Uint cflushdlg: mythreadproc (lpvoid pparam)

{

While (waitforsingleobject (g_event, mt_interval )! = Wait_object_0)

{

..................

}

Return 0;

}

In this thread function, you can set mt_interval to control how often the function body of this thread is executed. When the event is in a non-signal state, the function is executed once every mt_interval, when the event is set to a signal state, the thread is executed (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.