Skip to main content
GameDev.net gamedev.net
🔒 Locked

Difference mutex, event code snipped

Started by Zoomby Mar 12, 2008 at 3:28 AM 2 replies 2k views
Original Post
Zoomby
Zoomby
Hi, WINAPI: what is the difference between these two synchronization methods? Is there any in what it is doing? Is there a difference in how it is behaving under the hood? And I wonder: Is a critical section simply implemented with a mutex?

ma = CreateMutex(NULL,FALSE,TEXT("M1"));

WaitForSingleObject(ma,INFINITE);
...
ReleaseMutex(ma);


ea = CreateEvent(NULL,FALSE,TRUE,TEXT("E1"));

WaitForSingleObject(ea,INFINITE);
...
SetEvent(ea);

Bye, Zoomby
Evil Steve
Evil Steve
Quote:
Original post by Zoomby
And I wonder:
Is a critical section simply implemented with a mutex?
I'm not really sure about the first question to be honest, I've always used events signalling between threads. As for the critical section, the CRITICAL_SECTION is declared as:
typedef struct _RTL_CRITICAL_SECTION {    PRTL_CRITICAL_SECTION_DEBUG DebugInfo;    //    //  The following three fields control entering and exiting the critical    //  section for the resource    //    LONG LockCount;    LONG RecursionCount;    HANDLE OwningThread;        // from the thread's ClientId->UniqueThread    HANDLE LockSemaphore;    ULONG_PTR SpinCount;        // force size on 64-bit systems when packed} RTL_CRITICAL_SECTION, *PRTL_CRITICAL_SECTION;
So there's a semaphore in there along with other members.
Zoomby
Zoomby
Thanks Steve.

Has anyone an answer to my first question?
emeyex
emeyex
From the Win32 docs:

Quote:

A critical section object provides synchronization similar to that provided by a mutex object, except that a critical section can be used only by the threads of a single process. Event, mutex, and semaphore objects can also be used in a single-process application, but critical section objects provide a slightly faster, more efficient mechanism for mutual-exclusion synchronization (a processor-specific test and set instruction). Like a mutex object, a critical section object can be owned by only one thread at a time, which makes it useful for protecting a shared resource from simultaneous access. There is no guarantee about the order that threads obtain ownership of the critical section, however, the system is fair to all threads. Unlike a mutex object, there is no way to tell whether a critical section has been abandoned.


EDIT: This doesn't necessarily provide an answer for the difference between "Events" and "Mutexes"... this is the difference between CRITICAL_SECTION and CreateMutex. My hunch is that events and mutexes are more similar (can be used between processes, slightly heavier weight).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.