April 2020
Intermediate to advanced
412 pages
9h 58m
English
In our application, we are going to create two worker threads that will increment a shared counter, and let them run for a specific amount of time.
As a first step, we define two global atomic variables, running and counter:
std::atomic<bool> running{true};std::atomic<int> counter{0};
The running variable is a binary flag. When it is set to true, the worker threads should keep running. After it changes to false, the worker threads should terminate.
The counter variable is our shared counter. The worker threads will concurrently increment it. We use the fetch_add method that we already used in the Using atomic variables recipe. It is used to increment a variable atomically. In this recipe, we pass an additional argument, ...
Read now
Unlock full access