Exercise 2 of Lesson 8 looks have a potential TOCTOU issue

academy.nordicsemi.com/.../

The comparison of "increment_count + decrement_count != COMBINED_TOTAL" happens after test_mutex unlock.
It is possible that between comparison and test_mutex unlock, "increment_count" or "decrement_count" is incremented by another thread.
Then "increment_count" + "decrement_count" != "COMBINED_TOTAL".
When running in qemu, we can see  "increment_count" + "decrement_count" = 41.

It is a kind of TOCTOU.

To solve this problem, we can add a new variable sum = increment_count + decrement_count, and use sum to do comparison.

Related