Thursday, March 22, 2012

How does a mutex work? What does it cost? « Musing Dragoman

How does a mutex work? What does it cost? « Musing Dragoman: "What is a mutex?

A mutex, in its most fundamental form, is just an integer in memory. This memory can have a few different values depending on the that state of the mutex. Though usually when we speak of mutexes we also talk of the locks which use the mutex. The integer in memory is not very interesting, but the operations around it are.

There are two fundamental operations which a mutex must provide to be useful:

lock
unlock
unlock is a simple case since it’s usually just one function. Unlocking a mutex makes it available for another process to lock. lock on the other hand usually has several variants. In most cases we’d like to wait until we can lock the mutex, so the most common lock operation does exactly this. Other users may wish to only wait for a given period of time, and yet some other users may not want to wait at all. Thus lock has a few variants, all of which have the goal to lock the mutex."

'via Blog this'