Locking
Locking is a synchronization technique that controls concurrent access to a shared resource or protected operation.
Object-Oriented Software Design in C++
Mutual exclusion is a synchronization property that restricts access to a protected resource so that only one thread can enter the critical operation at a time.
- How it is used
- Protects shared memory, files, printers, and other resources in multithreaded applications.
- How deep it goes
- Explains critical regions, mutex lock-block-unlock behavior, scheduling variability, and deadlock risk, while deferring condition-variable synchronization.
- Examples
- Uses three C++ threads writing different strings character by character to a shared print stream.
- What you can do after
- Apply it via C++ mutexes to guard critical regions and diagnose interleaved output caused by unprotected shared resources.
Read how Object-Oriented Software Design in C++ explains locking →
Learn Go with Pocket-Sized Projects
Mutual exclusion is the property that prevents more than one execution context from accessing a protected resource at the same time.
- How it is used
- Protects shared cache, repository, and solver state from concurrent access by goroutines.
- How deep it goes
- Explains mutex placement, locking, deferred unlocking, and copying restrictions through implementations, including scope and performance tradeoffs.
- Examples
- Uses a TTL cache, an in-memory game repository, and a concurrent maze solver to demonstrate locking.
- What you can do after
- Apply it via Go’s sync.Mutex to protect shared state and test for races, while recognizing that locking alone does not prevent every deadlock.
Read how Learn Go with Pocket-Sized Projects explains locking →
Modern C, Third Edition
A mutex is a mutual-exclusion synchronization object that allows only one thread at a time to hold a protected critical section.
- How it is used
- Protects critical sections accessing shared game state across input, accounting, update, and drawing threads.
- How deep it goes
- Explains lock operations, visibility, condition waits, and lock-order deadlock, while leaving extensive liveness treatment out of scope.
- Examples
- Uses game-board input and accounting threads, plus two threads acquiring two mutexes in opposite orders.
- What you can do after
- Apply it via C mutex functions to protect shared critical sections and use consistent lock ordering.
Software Design for Python Programmers
Mutual exclusion is a synchronization constraint that permits only one thread or execution path at a time to access a protected resource or critical operation.
- How it is used
- Protects shared resources in explicitly multithreaded applications from conflicting concurrent access.
- How deep it goes
- Explains critical regions, blocking acquisition, release, Python Lock usage, reader-writer coordination, and deadlock risks.
- Examples
- Uses interleaved multithreaded printing and a reader-writer meter simulation with technicians and loggers.
- What you can do after
- Operate/configure Python threading.Lock to guard critical regions and diagnose race conditions, blocking, deadlocks, and scheduling-sensitive output.
Read how Software Design for Python Programmers explains locking →
Latency
A mutex lock is an exclusive synchronization primitive that permits only one concurrent execution context to access a protected resource at a time.
- How it is used
- Uses locking to protect shared data while assessing its impact on low-latency concurrent systems.
- How deep it goes
- Explains mutex acquisition and blocking, benchmarks contention, and compares read–write locks and wait-free synchronization.
- Examples
- Demonstrates a shared counter updated by 100 Rust threads and benchmarks a mutex-protected bounded queue.
- What you can do after
- Apply locking via Rust’s std::sync::Mutex to protect shared data and evaluate contention using latency measurements.
100 Go Mistakes and How to Avoid Them
A mutex is a synchronization primitive that provides mutual exclusion by allowing at most one execution to enter a protected section at a time.
- How it is used
- Synchronizes goroutines accessing shared mutable data and contrasts locking with communication through channels.
- How deep it goes
- Explains critical sections and diagnoses map aliasing and copied mutexes, with contextual fixes rather than exhaustive selection rules.
- Examples
- Uses concurrent counter increments and a customer-balance cache to expose incorrect locking and demonstrate fixes.
- What you can do after
- Diagnose data races and copied locks using Go's race detector and go vet, then apply appropriate mutex fixes.
Read how 100 Go Mistakes and How to Avoid Them explains locking →
Java Persistence with Spring Data and Hibernate epub
Locking is a concurrency-control technique that restricts access to data while a transaction holds a lock on it.
- How it is used
- Controls concurrent database access to support transaction isolation in Java Persistence applications.
- How deep it goes
- Explains lock acquisition, duration, dialect-specific SQL, transaction costs, and deadlock mitigation.
- Examples
- Uses a locked category update and concurrent item updates in opposite orders to illustrate acquisition and deadlocks.
- What you can do after
- Apply it via JPA and Hibernate to request pessimistic locks and reduce deadlock risk.
Read how Java Persistence with Spring Data and Hibernate epub explains locking →
Modern C
Mutual exclusion is a synchronization property ensuring that at most one thread accesses a protected region at a time.
- How it is used
- Protects shared game data while coordinating concurrent input, update, accounting, and drawing threads.
- How deep it goes
- Explains acquisition, release, visibility, alternative lock calls, and condition-wait loops through thread code.
- Examples
- Uses a shared game matrix and accounting, update, drawing, and input threads to illustrate protected work and waiting.
- What you can do after
- Apply locking via C’s threads.h mutex interfaces to shared data and condition-based thread coordination.
Grokking Concurrency
A lock is a synchronization mechanism that restricts access to a shared resource so that competing tasks cannot modify it simultaneously.
- How it is used
- Protects shared resources and critical sections from conflicting concurrent operations.
- How deep it goes
- Explains acquisition, blocking, and release through examples, and discusses lock failures and synchronization overhead.
- Examples
- Uses a shared printer, concurrent bank transactions, and dining philosophers competing for chopsticks.
- What you can do after
- Apply it via Python’s threading.Lock to protect shared account operations.
PostgreSQL Mistakes and How to Avoid Them
A database lock is a concurrency control mechanism that permits or prevents access to a database object by other transactions.
- How it is used
- Explains how PostgreSQL coordinates concurrent table and row access and why idle transactions become production performance hazards.
- How deep it goes
- Explains lock levels, conflicts, and a blocking sequence, but stops short of catalog-based lock diagnosis or exhaustive lock-mode reference.
- Examples
- Uses an idle SELECT transaction, ALTER TABLE, and a later SELECT to demonstrate cascading lock blockage.
- What you can do after
- Diagnose why ordinary-looking reads block schema changes and why later queries queue behind the blocked operation.
Read how PostgreSQL Mistakes and How to Avoid Them explains locking →
The Coder Cafe
Mutual exclusion is the condition that prevents more than one thread from accessing a protected resource or code section simultaneously.
- How it is used
- Protects shared resources from simultaneous access by multiple threads.
- How deep it goes
- Defines mutex ownership and walks through locking, waiting, and unlocking; stops short of implementation internals.
- Examples
- Uses a room key and a shared price read-and-write example to show exclusion and its limits.
- What you can do after
- Apply mutex locking to shared reads and writes, while recognizing that execution order can still affect results.
Troubleshooting Java: Read, debug, and optimize JVM applications
A synchronization mechanism that controls concurrent access to a protected resource or code region.
- How it is used
- Uses sampling and lock profiling to locate thread waits, identify involved monitors, and assess performance impact.
- How deep it goes
- Explains synchronized-block waits and profiler signals, then traces lock counts, durations, monitors, and thread relationships.
- Examples
- Uses producer and consumer threads contending on one ArrayList monitor, with roughly 3,700 blocks per thread.
- What you can do after
- Diagnose lock-related slowdowns by sampling first, then profiling events to identify affected threads, monitors, and blocking relationships.
Read how Troubleshooting Java: Read, debug, and optimize JVM applications explains locking →
Advanced Algorithms and Data Structures
A lock is a synchronization primitive that coordinates access to shared resources by allowing mutually exclusive ownership.
- How it is used
- Coordinates concurrent cache access and protects database-backed updates from inconsistent reads and writes.
- How deep it goes
- Explains read/write compatibility, try-finally release, deadlock prevention, and latency trade-offs through cache implementations.
- Examples
- Uses Java LFU-cache get and set methods, lock-compatibility cases, and database cursor updates as concrete examples.
- What you can do after
- Apply it via read and write locks with guaranteed release around shared cache operations.
Read how Advanced Algorithms and Data Structures explains locking →
Elixir in Action, Third Edition
A cluster-wide lock is a distributed mutual-exclusion mechanism that prevents competing operations on different nodes from proceeding simultaneously.
- How it is used
- Coordinates cluster-wide critical sections and prevents competing processes on different nodes from operating simultaneously.
- How deep it goes
- Explains lock identity, acquisition, waiting, release, transactional wrapping, and registration internals without detailing broader failure handling.
- Examples
- Demonstrates competing :global.set_lock/1 calls across two nodes, explicit deletion, and :global.trans/2 around protected work.
- What you can do after
- Operate/configure cluster-wide locks with :global.set_lock/1, :global.del_lock/1, and :global.trans/2.
Go by Example
- How it is used
- Protects a shared in-memory link map used concurrently by HTTP-request goroutines.
- How deep it goes
- Explains mutex locking behavior and develops RWMutex read/write protection with deferred release.
- Examples
- Uses a Shortener service whose Shorten and Resolve methods guard an in-memory link map.
- What you can do after
- Apply it via sync.RWMutex to make shared map access safe for concurrent goroutines.
Go in Practice, Second Edition
- How it is used
- Protects shared data from inconsistent concurrent access by allowing goroutines to use a resource exclusively.
- How deep it goes
- Explains locking and unlocking, demonstrates a word-counter race, and shows a mutex-based implementation.
- Examples
- Uses a concurrent word counter that tallies files into a shared map and demonstrates race-detection output.
- What you can do after
- Apply sync.Mutex to protect shared data and use Go’s race detector to identify concurrent access.
Grokking Software Architecture
A database mechanism that restricts concurrent access to data to protect consistency during operations.
- How it is used
- Coordinates team infrastructure deployments to prevent concurrent updates from corrupting shared Terraform state.
- How deep it goes
- Explains automatic locking and blocking concurrent deployments, noting a database table may support the lock.
- Examples
- Uses two developers deploying simultaneously, with remote state stored in an AWS S3 bucket.
- What you can do after
- Operate Terraform team deployments by waiting for an active infrastructure update to finish before deploying.
ASP.NET Core in Action, 3e
Database locking is a concurrency-control technique that restricts simultaneous access to shared database state.
- How it is used
- Coordinates clustered background jobs so one application instance handles each trigger.
- How deep it goes
- Explains the operational purpose and database limitations but stops short of describing lock internals.
- Examples
- Uses Quartz.NET jobs and triggers, SQL Server persistence, a clustering configuration, and an EF Core schema migration.
- What you can do after
- Operate clustered Quartz.NET services that coordinate trigger handling through a shared database.
Go in Action, Second Edition
A lock is a synchronization mechanism that restricts concurrent access to a protected resource.
- How it is used
- Protects shared data when multiple goroutines concurrently read or modify a resource.
- How deep it goes
- Explains race conditions, exclusive and read locks, and an ArticleStore implementation, while deferring broader synchronization internals.
- Examples
- Demonstrates an ArticleStore, RSS feed fetchers, duplicate filtering, sorted results, read locks, write locks, and WaitGroup coordination.
- What you can do after
- Apply it via sync.Mutex or sync.RWMutex to protect shared data and maintain consistency across concurrent operations.
Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu
State locking is the coordination mechanism that prevents concurrent Terraform operations from modifying the same state simultaneously.
- How it is used
- Coordinates Terraform operations so concurrent processes cannot modify shared state simultaneously.
- How deep it goes
- Explains lock behavior, the unsafe override, speculative-plan exception, and backend-specific configuration requirements.
- Examples
- Uses simultaneous apply runs, the `-lock=false` flag, speculative plans, S3, and the local backend.
- What you can do after
- Operate/configure Terraform backends with locking and recognize when disabling locks is acceptable for non-mutating plans.
Read how Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu explains locking →
Learn Rust in a Month of Lunches
Mutual exclusion is a concurrency property that allows at most one thread to access a protected resource at a time.
- How it is used
- Protects shared mutable values when multiple threads may access them.
- How deep it goes
- Explains guards, scope-based release, drop(), deadlock, and try_lock(), but not broader synchronization design.
- Examples
- Uses integer mutation, scoped guards, explicit drop(), repeated increments, and a deliberately blocking second lock.
- What you can do after
- Apply it via Rust's std::sync::Mutex to guard shared updates and avoid indefinite waits with try_lock().
Read how Learn Rust in a Month of Lunches explains locking →
Also covered in
- Learn Concurrent Programming with Go
- Python Concurrency with asyncio
- C# Concurrency: Asynchronous and multithreaded programming
- Troubleshooting Java, Second Edition
- Rust Servers, Services, and Apps
- Fundamentals of CybersecurityIn early access (MEAP)
- Clojure: The Essential Reference
- Refactoring to Rust
- Grokking Simplicity
- 100 SQL Server Mistakes and How to Avoid Them
- Think Distributed Systems
- Data-Oriented Programming: Reduce software complexity
What to know first
- threadA thread is a distinct sequence of execution with its own control flow and state, managed or scheduled by a processor, runtime, or operating system.
- concurrencyConcurrency is the structuring or execution of multiple computational activities so their progress overlaps in time, whether or not they run simultaneously.
- multithreadingMultithreading is the execution of multiple threads of control within a program, concurrently through interleaving or simultaneously.
- race conditionA race condition is a concurrency defect in which program behavior or results depend on the timing or ordering of interacting operations.
Related concepts
- race conditionaddresses · A race condition is a concurrency defect in which program behavior or results depend on the timing or ordering of interacting operations.
- condition variableused by · A condition variable is a synchronization object that allows threads to wait for a shared-state condition and be notified when it may have changed.
- data raceaddresses · A data race is a concurrent access to shared data in which at least one access is a write and the accesses are not adequately synchronized.
- cnd_timedwaitused by · cnd_timedwait is a C threads-library function that atomically releases a mutex while waiting on a condition variable, then reacquires it after notification or an absolute timeout.
- shared resourceuses · A shared resource is data, an object, or another resource that can be accessed or used by multiple actors or execution contexts.
- async/awaitcontrasts with · Async/await is language syntax for defining asynchronous computations that can suspend at awaited operations and resume when those operations produce results.
Questions about locking
What is locking?
Locking is a synchronization technique that controls concurrent access to a shared resource or protected operation.
Which Manning books explain locking?
Object-Oriented Software Design in C++, Learn Go with Pocket-Sized Projects, Modern C, Third Edition, Software Design for Python Programmers, Latency, 100 Go Mistakes and How to Avoid Them, Java Persistence with Spring Data and Hibernate epub, Modern C and 13 more.
What should I know before learning locking?
Manning books that teach locking usually introduce thread, concurrency, multithreading and race condition first.
What is the difference between locking and async/await?
Locking is a synchronization technique that controls concurrent access to a shared resource or protected operation. Async/await is language syntax for defining asynchronous computations that can suspend at awaited operations and resume when those operations produce results.
What is locking also called?
Locking is also called lock.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.




















