Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is the difference between SetEvent() and CCriticalSection class Lock() function. i cannot understand it. because it has same functionality confused me. any idea?
Posted

The SetEvent()[^] call, releases threads that are waiting for the event to be signalled. The CCriticalSection::Lock()[^] function waits for an object to be signalled. Thus they are not the same.
 
Share this answer
 
SetEvent() and Lock() are not similar. SetEvent() signals the Event object, and any waiting threads will notified it.
CriticalSection is normally used to access a shared resource[like heap memory accessed by two threads] within a process.

CriticalSection::Lock() gains access to a shared resource, if any other thread accesses the same resource[that thread already called CriticalSection::Lock() of the same CriticalSection], then CriticalSection::Lock() will blocked until other thread UnLock() the critical section.

I hope your question is the difference between Event and CriticalSection.

You can achieve functionality of a CriticalSection with an Event. But the reverse is not possible.

Event and CriticalSection is used for synchronization.
A CriticalSection is limited in the process, But event can be accessed from other process.
Therefore we can achieve synchronization of two threads in different processes with an Event. But CriticalSection can only synchronize threads within the process.

Event is a kernel mode object, and CriticalSection is user mode object. Therefore the processing time of CriticalSection functions are[EnterCriticalSection, LeaveCriticalSection] is less comparing to Event functions[WaitForSingleObject and WaitForMultipleObjects].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900