We have a member method (bool try()) which is supposed to be thread-safe which decrements the variable m_count if it is greater than 0 and we try to avoid mutexes and instead use fetch_sub and atomic_thread_fence.
struct Test {
bool try_wait() {
if (m_count<1)
return false;
int count = m_count.fetch_sub(1, std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_acquire);
return true;
}
Test():m_count(1) {}
private:
std::atomic<int> m_count;
}
We want to ensure that m_count never becomes less than 0 and that try returns true if m_count is decremented. Above two threads can decrement m_count from 1 to -1 and that is not acceptable.
Aucun commentaire:
Enregistrer un commentaire