January 2020
Intermediate to advanced
454 pages
11h 25m
English
In this recipe, we will learn how to use C++'s atomic data types. Atomic data types are limited to simple data types such as integers, and since these data types are extremely complicated to implement, the only operations that are supported are simple operations such as add, subtract, increment, and decrement.
Let's take a look at a simple example that not only demonstrates how to use an atomic data type in C++, but also demonstrates why atomic data types are so important:
#include <atomic>#include <thread>#include <iostream>int count{};std::atomic<int> atomic_count{};void foo(){ do { count++; atomic_count++; } while (atomic_count < 99999);}int main(void){ std::thread t1{foo}; std::thread t2{foo}; t1.join(); t2.join(); std::cout ...