Mutex v/s Semaphore v/s Spinlock


– All of these are used for synchronization


Mutex provides one person to access a single resource at a time, others must wait in a queue. Once this person is done, the guy next in the queue acquire the resource.
So access is serial, one guy after other. There is a context switch of the requester thread if mutex is unavailble.

Semaphore are useful if multiple instances (N) of a resource is to be shared among a set of users. As soon as all N resources are acquired, any new requester has to wait. Since there is no single lock to hold, there is as such no ownership of a semaphore.

Spinlock is an aggressive mutex. In mutex, if you find that resource is locked by someone else, you (the thread/process) switch the context and start to wait (non-blocking).

Whereas spinlocks do not switch context and keep spinning. As soon as the resource is free, they go and grab it. In this process of spinning, they consume many CPU cycles. Also, on a uni-processor machine, they are useless and perform very badly.


Published by


A voyager on the journey to technology and art of software development. Pursuing arts, music, photography, and ways to live life on the edge

4 thoughts on “Mutex v/s Semaphore v/s Spinlock

  1. You make it look as spinlocks are bad, and actually they are good if processes release the lock quickly because spinlocks don’t make the OS reschedule them, thus being efficient if the wait time of locking is little.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.