14.5 Shared State Safety
Running multiple tasks concurrently is all fun and games until concurrent tasks need to access the same variables. What if multiple tasks need to read and modify the same piece of data? How can you ensure intertask value consistency in such cases?
That’s what this section is going to be about. If two tasks try to read and write to the same data simultaneously, it leads to a data race, one of the most common problems in concurrent programming. Data races are also notoriously hard to spot and debug as they occur when concurrent tasks are active. You can’t always review the problem in slow motion using debug tools.
Listing 14.32 demonstrates a simple case of data races, in which two concurrent tasks attempt to access ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access