mutex


What is inner structure of the mutex?


I understood that I use mutexes but I don't understood how it works. I mean how it works inside.
How does it exclude another threads or processes when it locked, how does it know who is current owner and etc?
Can a someone post a link on good article about it? Just I found only how it works from programmer's viewpoint, not its realization.
Depending on the platform/language it is implemented differently. I find Go's implementation nice to follow (not easy to follow, but more or less clear)
There, a mutex is a struct with two fields, 32 bit integers:
type Mutex struct {
state int32
sema uint32
}
If the mutex is not already locked, then the operation basically amounts to a compare and swap instruction:
// Fast path: grab unlocked mutex.
if atomic.CompareAndSwapInt32(&m.state, 0, mutexLocked) {
if race.Enabled {
race.Acquire(unsafe.Pointer(m))
}
return
}
Compare and swap, from wikipedia, is:
In computer science, compare-and-swap (CAS) is an atomic instruction used in multithreading to achieve synchronization. It compares the contents of a memory location to a given value and, only if they are the same, modifies the contents of that memory location to a new given value.
The function signature looks like this:
func CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool)
(How does this work, you ask? How does Compare and Swap work?)
If the mutex is not unlocked, then the Lock() method will have to wait (block the thread) until it can successfully swap the values at the Mutex's state.
One thing to keep in mind, is that a specific and immutable address in memory is essential for keeping a Mutex working, that is, you can't copy a mutex's value and pass it around -- it's/its a pointer to a shared space in memory.
There are resources out there for building a lock itself (although I find golangs more or less short implementation good enough)
http://pages.cs.wisc.edu/~remzi/OSTEP/threads-locks.pdf
https://www.andrew.cmu.edu/course/15-440-s12/applications/ln/lecture6.html
Implementation is platform and language specific.
Wikipedia lists several popular mutex algorithms and hardware solutions.
https://en.wikipedia.org/wiki/Mutual_exclusion
Linux goes with futex https://en.wikipedia.org/wiki/Futex
Also check previous discussion
How are mutexes implemented?

Related Links

Why we need both mutexes and semaphores?
Mutex to Semaphore
Why are two semaphores and one mutex required in solving bounded buffer instance of producer-consumer?
Differentiating between Binary semaphore and Mutex using same code
Why is this Mutex solution incorrect?
I want to check for correct mutex
Are atomic operations enough for a mutex?
Distributed systems - signal LIFO
How to use a mutex in FreeRTOS?
How to make method not require mutable self for locked Mutex?
What is the difference about Mutual Exclusion, between Monitor and Semaphore
Mutual Exclusion case
When are mutexs required for producer/consumer problems?
What are the practical uses of semaphores?
Is ISR or mutex task has the higher priority?
Producer-consumer and how it works

Categories

HOME
.htaccess
videochat
audio
perl
local-storage
form-submit
unix
set
winzip
sharppcap
bloomberg
custom-attributes
linux-device-driver
metal
boost
mariadb
timezone
chapel
amazon-sqs
uinavigationcontroller
codeception
codecvt
jlabel
prefix
trackjs
rinsim
serenity-bdd
gulp-sass
nattable
tv
summary
xbuild
oracle-data-integrator
bing-api
graphic
pi4j
coupon
wasapi
spring-4
modeling
choropleth
python-tesseract
vim-syntax-highlighting
kinvey
reactive-streams
gsutil
transfer-function
web.py
enzyme
notnull
watchdog
xilinx-edk
fancytree
fluent-nhibernate
paypal-adaptive-payments
mosync
javassist
ikvm
android-build
playstation
data-transfer
voxel
pandorabots
rodbc
user-mode-linux
bitcount
rebase
cirqus
steroids
sql-server-2008-express
approximation
treenode
srv
componentone
image-manipulation
iec61131-3
wtl
controltemplate
n-tier
rhodecode
firstdata
tiles2
scite
statamic
filtered-lookup
stackmob
kendo-tooltip
perl-critic
mdbtools
jquery-ui-selectable
grit
customer-support
fatwire
alpha-five
.lrc
endl
soaplite
continuations
eda
set-include-path
windows-live-writer
asp.net-mvc-2-validation
projective-geometry
trackback

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile