Event functions SetEvent, PulseEvent and WaitForSingleObject

Source: Internet
Author: User

The event object in the kernel object of the system is commonly used when synchronizing between processes and threads, discovering that it has two departure functions, one is setevent, and the other is a pulseevent,

The difference between the two is:

SetEvent to set the event object to a signaled state, while PulseEvent also sets the specified event to a signaled state,

The difference is that if it is a manual reset event, all suspended threads that are waiting for the event go into the active state, the function then sets the event back and returns, and if it is an automatic reset event, the suspended single thread that is waiting for the event goes into the active state, and the event is then set back to no signal and the function returns.

In other words, in the automatic reset mode pulseevent and setevent the role of no difference, but in the manual mode pulseevent There are obvious differences, it can be easier to control the procedure is a single step, or continuous walk. If you let the loop in accordance with the requirements of the pulseevent, if you want to let the cycle of continuous operation with SetEvent, in the request to stop the place to send a resetevent OK.

The role of a critical section: avoid data in critical areas (typically shared resources) accessed concurrently by different threads, enabling synchronous operation of threads. It is guaranteed that only one thread that enters the critical section will end up with access to the shared resource. The critical section uses the CRITICAL_SECTION structure object to protect shared resources when used, and uses the EnterCriticalSection () and leavecriticalsection () functions respectively to identify and release a critical section. The CRITICAL_SECTION structure object used must be initialized by InitializeCriticalSection () before it can be used, and you must ensure that any code in any thread that attempts to access this shared resource is protected by this critical section. Otherwise the critical section will not play its rightful role, and the shared resources may still be compromised.

Here's how to use the critical section:

*.h header file:

#include <winbase.h>

Critical_section m_cs_test; < Defining a critical section object

*.cpp How to use the source file:

Initializing a critical section object within a class's constructor

The function must be called before any thread calls the EnterCriticalSection function, otherwise the result will be difficult to predict

:: InitializeCriticalSection (&m_cs_test);

Releasing a critical section object within a class's imaginary function

::D eletecriticalsection (&m_cs_test);

Use method One:

Start the lock in the function you want to use:

Ccriticalsectionlock Lock (M_cs_test);

Automatically unlocked after the end of the function's lifetime.

Use Method Two:

Enter start lock data: protection of shared resources is only called by one thread

EnterCriticalSection (&m_cs_test);

...//do something you want to

Leave locked data: Allow shared resources to be called by other threads

LeaveCriticalSection (&m_cs_test);

Attention:

When using a critical section, it is generally not allowed to run too long, as long as the thread entering the critical section has not left, all other threads attempting to enter this critical section will be suspended to enter the waiting state, and will affect the performance of the program to some extent. In particular, it is important not to include operations that wait for user input or some other external intervention into the critical section. If you enter a critical section and you have not released it, it will also cause other threads to wait for a long time. In other words, no matter what happens after executing the entercriticalsection () statement into the critical section, you must ensure that the matching leavecriticalsection () can be executed. You can ensure the execution of the LeaveCriticalSection () statement by adding structured exception handling code. Although critical section synchronization is fast, it can only be used to synchronize threads within this process and not to synchronize threads in multiple processes.

WaitForSingleObject function:

DWORD WaitForSingleObject (HANDLE hhandle,dword dwmilliseconds);

Parameters:
Hhandle: A handle to the specified object or event;
Dwmilliseconds: The wait time, in milli-units, is returned when the wait time is exceeded. If the parameter is set to 0, the function returns immediately, and if set to infinite, the function is not returned until it has a signal.
return value:
If this function succeeds, the function's return identifies the event that caused the function to return. The return values are as follows:
Wait_abandoned (0x00000080l)
The specified object is a mutex object that is not freed before the thread that owns the object is finished. Ownership of the mutex object is granted to the thread that called the function. The mutex object is set to a signal-free state.
WAIT_OBJECT_0 (0x00000000l)
The specified object has a signaled state.
Wait_timeout (0x00000102l)
The specified object is in a no-signal state over the wait time
If it fails, return to wait_failed;

In the program, the time of each thread is controlled!

HANDLE HThread1;

Hthread1=createthread (Null,0,datahandle, (LPVOID) &d,0,null); Creating thread-processing data

if (WaitForSingleObject (HThread1,

1000//time limit

)! = WAIT_OBJECT_0)

{

Writelog (logFile, "terminating thread");

TerminateThread (hthread1,0);

}

Event functions SetEvent, PulseEvent and WaitForSingleObject

Related Article

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.