Blockchain & Cryptocurrency Glossary

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

  • search-icon Clear Definitions
  • search-icon Practical
  • search-icon Technical
  • search-icon Related Terms

Mutex Pattern

1 min read
Pronunciation
[myoo-teks pat-ern]
Analogy
A mutex is like a single‑occupancy bathroom key—you must hold the key to enter, and no one else can enter until you return it.
Definition
A mutual‑exclusion design that prevents reentrancy by ensuring only one execution context can enter a critical section at a time.
Key Points Intro
Mutex Pattern avoids concurrent issues via:
Key Points

Lock flag: A boolean `locked` guarding critical code.

Modifier: `nonReentrant` sets lock before, clears after execution.

Reentrancy protection: Prevents nested calls into protected functions.

Minimal scope: Only wrap sensitive sections to reduce risk.

Example
``` modifier nonReentrant { require(!locked); locked = true; _; locked = false; } ```
Technical Deep Dive
Define `bool locked` storage. The `nonReentrant` modifier checks `require(!locked)`, sets `locked = true`, executes function body, then resets `locked = false`. Apply to functions that perform external calls and state updates. For multiple mutexes, use an enum or separate flags.
Security Warning
If function reverts after setting `locked`, ensure `locked` resets in a `finally`‑style mechanism; use `try/catch` or separate internal functions.
Caveat
Single global mutex can block unrelated functions; consider fine‑grained locks.

Mutex Pattern - Related Articles

No related articles for this term.