I. Synchronous and Asynchronous Programming

Synchronous and asynchronous programming are two different approaches to executing code. Here are the key differences between the two:

Example integer operation with show the data race

Example integer_operation.cpp the 10 threads with 100,000 increments that should give a final result of 1 million but the result will diferrent bellow:

integer_operation.cpp

// A shared variable is modified by multiple threads
// Integer operations take a single instruction
// Is there a data race?
#include <thread>
#include <iostream>
#include <vector>

int counter = 0;

void task()
{
    for (int i = 0; i < 100'000; ++i) {
        ++counter;
    }
}

int main()
{
    std::vector<std::thread> tasks;

    for (int i = 0; i < 10; ++i)
        tasks.push_back(std::thread(task));

    for (auto& thr: tasks)
        thr.join();

    std::cout << counter << '\n';
}

Output

258413 // data races -> the 10 threads with 100,000 increments that should give a final result of 1 million but the result will diferrent

References

  1. https://en.cppreference.com/w/cpp/atomic/atomic_flag/atomic_flag
  2. https://en.cppreference.com/w/cpp/atomic/atomic_flag
  3. https://learn.microsoft.com/en-us/cpp/standard-library/atomic?view=msvc-170
  4. James Raynard, Learn Multithreading with Modern C++ Udemy.