mercredi 19 février 2020

std::atomic use in if condition but as atomic

I want to write something like below code:

std::atomic<bool> isReqProcess(false);

Thread 1

if(isReqProcess.load(std::memory_order_relaxed) == false)
{
  isReqProcess.store(true, std::memory_order_relaxed);
  ......
  semaphore.signal()
}

Thread 2

semaphore.wait()
.........
isReqProcess.store(false, std::memory_order_relaxed);

I am only doubting that Thread 1 doing both read and set operation - so again it is a race if Thread 1 is called multiple times it might not achieve the objective - is there a way that in Thread 1 if condition and the load and store of atomic variable can be made atomic?

Aucun commentaire:

Enregistrer un commentaire