Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mutex vs Event in Windows

can somebody please explain what is the difference if I do

mutex = createMutex
waitForSingleObject
Release(mutex)

and

event = createEvent
waitForSingleObject
Release(event)

I'm so confused, can I use both versions for the synchronization? thanks in advance for any help

like image 733
yeap Avatar asked Aug 02 '11 14:08

yeap


1 Answers

You use a mutex to ensure that only one thread of execution can be accessing something. For example, if you want to update a list that can potentially be used by multiple threads, you'd use a mutex:

acquire mutex
update list
release mutex

With a mutex, only one thread at a time can be executing the "update list".

You use a manual reset event if you want multiple threads to wait for something to happen before continuing. For example, you started multiple threads, but they're all paused waiting for some other event before they can continue. Once that event happens, all of the threads can start running.

The main thread would look like this:

create event, initial value false (not signaled)
start threads
do some other initialization
signal event

Each thread's code would be:

do thread initialization
wait for event to be signaled
do thread processing
like image 88
Jim Mischel Avatar answered Sep 22 '22 11:09

Jim Mischel