[ACCEPTED]-Difference between event object and condition variable-synchronization

Accepted answer
Score: 26

Event objects are kernel-level objects. They 32 can be shared across process boundaries, and 31 are supported on all Windows OS versions. They 30 can be used as their own standalone locks 29 to shared resources, if desired. Since 28 they are kernel objects, the OS has limitations 27 on the number of available events that can 26 be allocated at a time.

Condition Variables 25 are user-level objects. They cannot be 24 shared across process boundaries, and are 23 only supported on Vista/2008 and later. They 22 do not act as their own locks, but require 21 a separate lock to be associated with them, such 20 as a critical section. Since they are user- objects, the 19 number of available variables is limited 18 by available memory. When a Conditional 17 Variable is put to sleep, it automatically 16 releases the specified lock object so another 15 thread can acquire it. When the Conditional 14 Variable wakes up, it automatically re-acquires 13 the specified lock object again.

In terms 12 of functionality, think of a Conditional 11 Variable as a logical combination of two 10 objects working together - a keyed event and a lock 9 object. When the Condition Variable is 8 put to sleep, it resets the event, releases 7 the lock, waits for the event to be signaled, and 6 then re-acquires the lock. For instance, if 5 you use a critical section as the lock object, SleepConditionalVariableCS() is 4 similar to a sequence of calls to ResetEvent(), LeaveCriticalSection(), WaitForSingleObject(), and 3 EnterCriticalSection(). Whereas if you use a SRWL as the lock, SleepConditionVariableSRW() is 2 similar to a sequence of calls to ResetEvent(), ReleaseSRWLock...(), WaitForSingleObject(), and 1 AcquireSRWLock...().

Score: 2

They are very similar, but event objects 7 work across process boundaries, whereas 6 condition variables do not. From the MSDN documentation on condition variables:

Condition 5 variables are user-mode objects that cannot 4 be shared across processes.

From the MSDN documentation on event objects:

Threads 3 in other processes can open a handle to 2 an existing event object by specifying 1 its name in a call to the OpenEvent function.

Score: 1

The most significant difference is the Event 14 object is a kernel object and can be shared 13 across processes as long as it is alive 12 when processes/threads are trying to acquire, on 11 the contrary, Condition variable is a user 10 mode object which is light(only has same 9 size as a pointer and has nothing additional 8 to be released after using it) and has better 7 performance.

Typically, condition variable 6 is often used along with locks, since we 5 need to keep data synchronized properly. When 4 considering Condition Variable, we treat 3 it like keyed events which was improved 2 since Vista.

Joe duffy has a blog post http://joeduffyblog.com/2006/11/28/windows-keyed-events-critical-sections-and-new-vista-synchronization-features/ that 1 explained more detailed information.

More Related questions