Similarities and differences between spinlock, mutex and semaphore.

spinlock
========
1. better to use when contention is heavy
2. when one task works in critical section, other tasks don't go to sleep
3. critical section can be used by at most one task
4. useful in multi-processor system but not in single processor system
5. useful when critical section is very small
6. if not acquired, spinlock doesn't make the task sleep, so it doesn't force a context switch

mutex ( ~ binary semaphore)
=====
1. better to use when contention is not heavy
2. when one task works in critical section, other tasks go to sleep
3. critical section can be used by at most one task
4. useful in both multi-processor system and in single processor system
5. useful when critical section is not small
6. if not acquired, mutex makes the task sleep, so it forces a context switch

semaphore ( = counting semaphore)
=========
1. better to use when contention is not heavy
2. when one task works in critical section, other tasks go to sleep
3. critical section can be used by at most N tasks
4. useful in both multi-processor system and in single processor system
5. useful when critical section is not small
6. if not acquired, semaphore makes the task sleep, so it forces a context switch

No comments: