liveBook · Manning

Critical data

As explained in Modern C

Critical data is shared program data whose concurrent access must be synchronized to avoid inconsistent or conflicting updates.

A shared game board becomes critical data when multiple threads may access it concurrently. Atomic objects can protect small shared values, but larger structures such as the board matrix Mv require a mutex and a clearly bounded critical section. section 18.4

Protecting a board update

The life data structure contains Mv, visited, and an mtx_t member. When the input thread handles 'b' or 'B' and creates a 3×3 cell cluster, mtx_lock acquires the mutex before accessing the protected data, and mtx_unlock releases it afterward. Other input cases modify only atomic objects and need no such mutex protection. section 18.4

Use critical-data protection with these rules:

  • Identify the shared data and the exact code path that accesses it. section 18.4
  • Acquire the same mutex before entering a protected region and release it afterward; mtx_trylock can avoid waiting, while mtx_timedlock limits the wait. section 18.4
  • Initialize shared data before concurrent access, prefer local thread data, and use _Atomic for small shared values. section 18.7
15    mtx_t mtx;    //< Mutex that protects Mv
16    cnd_t draw;   //< cnd that controls drawing
17    cnd_t acco;   //< cnd that controls accounting
18    cnd_t upda;   //< cnd that controls updating
19
20    void*restrict Mv;            //< bool M[n0][n1];
21    bool (*visited)[life_maxit]; //< Hashing constellations


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":2}],[{\"line\":0,\"ch\":6},{\"line\":0,\"ch\":11}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":2}],[{\"line\":1,\"ch\":6},{\"line\":1,\"ch\":11}],[{\"line\":2,\"ch\":6},{\"line\":2,\"ch\":11}],[{\"line\":3,\"ch\":6},{\"line\":3,\"ch\":11}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":2}],[{\"line\":1,\"ch\":6},{\"line\":1,\"ch\":11}],[{\"line\":2,\"ch\":6},{\"line\":2,\"ch\":11}],[{\"line\":3,\"ch\":6},{\"line\":3,\"ch\":11}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":2}],[{\"line\":1,\"ch\":6},{\"line\":1,\"ch\":11}],[{\"line\":2,\"ch\":6},{\"line\":2,\"ch\":11}],[{\"line\":3,\"ch\":6},{\"line\":3,\"ch\":11}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":2}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":2}],[{\"line\":5,\"ch\":6},{\"line\":5,\"ch\":10}],[{\"line\":5,\"ch\":11},{\"line\":5,\"ch\":19}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":2}]]"}
!@%STYLE%@!

Browse the structure declaration showing the mutex alongside the board matrix and visited-state data. section 18.4

How Modern C covers it

How deep it goes
Defines critical data and critical sections, then explains mutex acquisition, release, blocking, and linearizability.
Examples
Uses a multithreaded life-game board, its Mv matrix, visited state, and the input commands 'b' and 'B'.
What it assumes
Assumes concurrent threads and shared data, while distinguishing atomic objects from mutex-protected larger structures.
What you can do after
Operate/configure C mutex protection around code paths that access larger shared data.
How it connects
Connects critical data with mutexes, critical sections, atomic objects, shared data, initialization, and condition variables.

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.