Mutex
Locks
ü The
hardware solutions presented above are often difficult for ordinary programmers
to access, particularly on multi-processor machines, and particularly because
they are often platform-dependent.
ü Therefore
most systems offer a software API equivalent called mutex locks or simply
mutexes. ( For mutual exclusion )
ü The
terminology when using mutexes is to acquire a lock prior to entering a
critical section, and to release it when exiting.
ü Just as
with hardware locks, the acquire step will block the process if the lock is in
use by another process, and both the acquire and release operations are atomic.
ü Acquire
and release can be implemented as shown here, based on a boolean variable
"available":
ü One
problem with the implementation shown here, ( and in the hardware solutions
presented earlier ), is the busy loop used to block processes in the acquire
phase. These types of locks are referred to as spinlocks, because the CPU just
sits and spins while blocking the process.
ü Spinlocks
are wasteful of cpu cycles, and are a really bad idea on single-cpu
single-threaded machines, because the spinlock blocks the entire computer, and
doesn't allow any other process to release the lock. ( Until the scheduler
kicks the spinning process off of the cpu. )
ü On the
other hand, spinlocks do not incur the overhead of a context switch, so they
are effectively used on multi-threaded machines when it is expected that the
lock will be released after a short time.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.