I’m interested in how a mutex works. I understand their purpose as every website I have found explains what they do but I haven’t been able to understand what happens in this case:
There are two threads running concurrently and they try to lock the mutex at the same time.
This would not be a problem on a single core as this situation could never happen, but in a multi-core system I see this as a problem. I can’t see any way to prevent concurrency problems like this but they obviously exist.
Thanks for any help
A mutex, when properly implemented, can never be locked concurrently. For this, you need some atomic operations (operations that are guaranteed to be the only thing happening to an object at one moment) that have useful properties.
One such operation is
xchg(exchange) in the x86 architecture. For instancexchg eax, [ebp]will read the value at the addressebp, write the value ineaxto the addressebpand then seteaxto the read value, while guaranteeing that these actions won’t be interleaved with concurrent reads and writes to that address.Now you can implement a mutex. To lock, load
1intoeax, exchangeeaxwith the value of the mutex and look ateax. If it’s1, it was already locked, so you might want to sleep and try again later. If it’s0, you just locked the mutex. To unlock, simply write a value of0to the mutex.Please note that I’m glossing over important details here. For instance, x86’s
xchgis atomic enough for pre-emptive multitasking on a single processor. When you’re sharing memory between multiple processors (e.g. in a multi-core system), it won’t be enough unless you use thelockprefix (e.g.lock xchg eax, [ebp], rather thanxchg eax, [ebp]), which ensures that only one processor can access that memory while the instruction is executed.