views:

2244

answers:

12
+10  Q: 

What is a mutex?

A mutex is a programming concept that is frequently used to solve multi-threading problems. My question to the community:

What is a mutex and how do you use it?

+3  A: 

When you have a multi-threaded application, the different threads sometimes share a common resource, such as a variable or similar. This shared source often cannot be accessed at the same time, so a construct is needed to ensure that only one thread is using that resource at a time.

The concept is called "mutual exclusion" (short Mutex), and is a way to ensure that only one thread is allowed inside that area, using that resource etc.

How to use them is language specific, but is often (if not always) based on a operative system mutex.

Some languages doesn't need this construct, due to the paradigm, for example functional programming (Haskell, ML are good examples).

Now, go ask google how to use them! :)

Mats Fredriksson
+7  A: 

Mutual Exclusion. Here's the Wikipedia entry on it:

http://en.wikipedia.org/wiki/Mutual_exclusion

The point of a mutex is to synchronize two threads. When you have two threads attempting to access a single resource, the general pattern is to have the first block of code attempting access to set the mutex before entering the code. When the second code block attempts access, it sees the mutex is set and waits until the first block of code is complete (and un-sets the mutex), then continues.

Specific details of how this is accomplished obviously varies greatly by programming language.

DannySmurf
+1  A: 

@Iker: maybe going through the index of a book on distributed computing :)

Edit:

I was refering to Dalroth's other questions, all asked in the same 10 minutes: What is a semaphore?, What is a deadlock?, What is a race condition?.

OysterD
+1  A: 

Wikiepia is always a good start in these kinds of problems :)

http://en.wikipedia.org/wiki/Mutex

I remember writing a few simple programs in Java, that were using mutex. I'll post them if I'll find them :)

Kociub
+1  A: 

Check any book on multi-threaded programming.

Wikipedia article will get you started with the basics.

Misha M
+1  A: 

A Mutex is a mutually exclusive flag. It acts as a gate keeper to a section of code allowing one thread in and blocking access to all others. This ensures that the code being controled will only be hit by a single thread at a time. Just be sure to release the mutex when you are done. :)

Craig
+26  A: 

When I am having a big heated discussion at work, I use a rubber chicken which I keep in my desk for just such occassions. The person holding the chicken is the only person who is allowed to talk. If you don't hold the chicken you cannot speak. You can only indicate that you want the chicken and wait until you get it before you speak. Once you have finished speaking, you can hand the chicken back to the moderator who will hand it to the next person to speak. This ensures that people do not speak over each other, and also have their own space to talk.

Replace Chicken with Mutex and person with thread and you basically have the concept of a mutex.

Of course, there is no such thing as a rubber mutex. Only rubber chicken. My cats once had a rubber mouse, but they ate it.

Of course, before you use the rubber chicken, you need to ask yourself whether you actually need 5 people in one room and it would not just be easier with one person in the room on their own doing all the work. Actually, this is just extending the analogy, but you get the idea.

Xetius
lmao @ your reply
acidzombie24
Why can't all programming constructs and paradigms be broken down like this? This made my day...
cinqoTimo
+2  A: 

Nope, just asking some basic questions that I noticed were missing, and I consider it bad form to answer your own questions.

Dalroth
you may consider it bad form, but StackOverflow doesn't. To each his own - one persons opinion is just as useful as the next and in crowds, they help drive the world.
ScottCher
+5  A: 

In C#, the common mutex used is the Monitor. The type is 'System.Threading.Monitor'. It may also be used implicitly via the 'lock(Object)' statement. One example of its use is when constructing a Singleton class.

private static readonly Object instanceLock = new Object();
private static MySingleton instance;
public static MySingleton Instance
{
    lock(instanceLock)
    {
        if(instance == null)
        {
            instance = new MySingleton();
        }
        return instance;
    }
}

The lock statement using the private lock object creates a critical section. Requiring each thread to wait until the previous is finished. The first thread will enter the section and initialize the instance. The second thread will wait, get into the section, and get the initialized instance.

Any sort of synchronization of a static member may use the lock statement similarly.

Anthony Mastrean
+1  A: 

This is rep hunting: http://stackoverflow.com/questions/34519/what-is-a-semaphore

Lulu
+1  A: 

Good article on Mutex's and Semaphores - what makes them different, and why they might or might not be used given various conditions.

Adam Davis
+1  A: 

Mutexes are useful in situations where you need to enforce exclusive access to a resource accross multiple processes, where a regular lock won't help since it only works accross threads.

18hrs