The following C++ code compares the performance of std::atomic and std::mutex:
#include <atomic>
#include <mutex>
#include <iostream>
#include <chrono>
#include <thread>
const size_t size = 100000000;
std::mutex mutex;
bool var = false;
typedef std::chrono::high_resolution_clock Clock;
void testA()
{
std::atomic<bool> sync(true);
const auto start_time = Clock::now();
for (size_t counter = 0; counter < size; counter++)
{
var = sync.load();
//sync.store(true);
//sync.exchange(true);
}
const auto end_time = Clock::now();
std::cout << 1e-6*std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).count() << " s\n";
}
