liveBook · Manning

Partial update

As explained in C# Concurrency: Asynchronous and multithreaded programming

A partial update is an inconsistency that occurs when concurrent access observes some data values from before an update and some values from after it.

A partial update occurs when one thread changes related data while another thread reads between those changes, observing a mixture of old and new values. The inconsistency can be obvious with two assignments, but a single assignment may hide multiple memory operations. section 7.1

C# struct copies can compile into member-by-member writes, and wide values such as decimal can span multiple memory operations. Even long assignments depend on the process’s bitness, so apparent single-value updates are not universally safe. section 7.1 section 7.1

Protect the complete operation

Use one lock for both writing and reading related fields. SetXY updates _x and _y under the same lock, while GetXY reads both under that lock, preventing an observer from seeing an intermediate pair.

Locking individual fields or successive operations is insufficient when the values form one logical operation. Hold the lock across the entire invariant-preserving operation, while avoiding unnecessarily long critical sections. section 7.4

How C# Concurrency: Asynchronous and multithreaded programming covers it

How deep it goes
Explains compound assignments, compiler-generated struct copies, value-width limits, and lock-based protection, but not alternative synchronization mechanisms.
Examples
Uses paired x and y assignments, Guid copying, decimal and long assignments, and C# lock listings.
What it assumes
Assumes basic C# fields and multithreading, while teaching from zero how interleaving creates inconsistent observations.
What you can do after
Apply it via C# locks to protect every access participating in one logical update.
How it connects
Connects partial updates with locking, race conditions, synchronization, and the C# memory model.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of C# Concurrency: Asynchronous and multithreaded programming. The section links point to the parts of the book it draws on.