DELAPARRA
Index
Color
NL
Category: Programming paradigms

Mutex / Lock

Explained by Updated

What is Mutex / Lock?

Mutex / Lock: A mechanism ensuring only one thread accesses a critical section of shared data at a time. A lock or mutex protects a critical section, a piece of code that must not run in two places at once. The first process takes the lock, does its work and releases it; others wait their turn.

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about Mutex / Lock

How is Mutex / Lock used in practice?

Typical cases in business software are generating sequential invoice numbers, updating stock levels, processing a payment webhook exactly once, and running a scheduled job that must never overlap with itself.

What should you keep in mind with Mutex / Lock?

Locks come with risks. Hold them as briefly as possible, because waiting processes pile up. Always release them in a finally block or with a built-in helper, so an exception does not leave the lock taken. Use timeouts, and take multiple locks in a consistent order to avoid deadlocks. Often an atomic database operation or a unique constraint solves the problem more simply than an explicit lock.

At a glance

CategoryProgramming paradigms
Explained by
Updated

More Programming paradigms terms

All terms starting with M

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.