Mutex example / tutorial ?
Posted
by
Nav
on Stack Overflow
See other posts from Stack Overflow
or by Nav
Published on 2011-02-14T06:33:46Z
Indexed on
2011/03/01
15:25 UTC
Read the original article
Hit count: 306
I've noticed that asking questions for the sake of creating a reference list etc. is encouraged in SO. This is one such question, so that anyone Googling for a mutex tutorial will find a good one here.
I'm new to multithreading, and was trying to understand how mutexes work. Did a lot of Googling and this is the only decent tutorial I found, but it still left some doubts of how it works because I created my own program and the locking didn't work.
One absolutely non-intuitive syntax of the mutex is pthread_mutex_lock( &mutex1 );
, where it looks like the mutex is being locked, when what I really want to lock is some other variable. Does this syntax mean that locking a mutex locks a region of code until the mutex is unlocked? Then how do threads know that the region is locked? And isn't such a phenomenon supposed to be called critical section?
In short, could you please help with the simplest possible mutex example program and the simplest possible explanation on the logic of how it works? I'm sure this will help plenty of other newbies.
© Stack Overflow or respective owner