2016-03-24 18:13:37 +00:00
|
|
|
---
|
|
|
|
layout: pattern
|
|
|
|
title: Mutex
|
|
|
|
folder: mutex
|
|
|
|
permalink: /patterns/mutex/
|
2016-07-21 09:27:48 +03:00
|
|
|
categories: Concurrency
|
2016-03-24 18:13:37 +00:00
|
|
|
tags:
|
2019-12-13 21:09:28 +02:00
|
|
|
- Decoupling
|
2016-03-24 18:13:37 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
## Also known as
|
2019-12-13 21:09:28 +02:00
|
|
|
|
|
|
|
* Mutual Exclusion Lock
|
|
|
|
* Binary Semaphore
|
2016-03-24 18:13:37 +00:00
|
|
|
|
|
|
|
## Intent
|
|
|
|
Create a lock which only allows a single thread to access a resource at any one instant.
|
|
|
|
|
2019-12-07 20:01:13 +02:00
|
|
|
## Class diagram
|
2016-03-24 18:13:37 +00:00
|
|
|

|
|
|
|
|
|
|
|
## Applicability
|
|
|
|
Use a Mutex when
|
|
|
|
|
2019-12-13 21:09:28 +02:00
|
|
|
* You need to prevent two threads accessing a critical section at the same time
|
|
|
|
* Concurrent access to a resource could lead to a race condition
|
2016-03-24 18:13:37 +00:00
|
|
|
|
|
|
|
## Credits
|
|
|
|
|
2020-01-11 13:14:59 +05:30
|
|
|
* [Lock (computer science)](http://en.wikipedia.org/wiki/Lock_(computer_science))
|
|
|
|
* [Semaphores](http://tutorials.jenkov.com/java-concurrency/semaphores.html)
|