necatiergin / CONCURRENCY

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect Use of Memory Order for std::atomic

atalay11 opened this issue · comments

The acquire and release memory orders for std::atomic operations are used in the wrong places. They need to be switched to ensure correct synchronization. The code is located at atomic/synchronisation/sync_04.cpp.

Corrected code:

#include <atomic>
#include <syncstream>
#include <iostream>
#include <thread>

std::atomic<int> ready_flag{ false };
int svar = 0;

void consumer()
{
	while (!ready_flag.load(std::memory_order_acquire)) { // It was memory_order_release
		std::osyncstream{ std::cout }.put('.');
	}
	std::osyncstream{ std::cout } << '\n' << svar;
}


void producer()
{
	std::this_thread::sleep_for(std::chrono::milliseconds{ 50 });
	svar = 38764;
	ready_flag.store(true, std::memory_order_release); // It was memory_order_acquire
}

int main()
{
	std::thread th_c{ consumer };
	std::thread th_p{ producer };

	th_c.join();
	th_p.join();
}