Skip to content

Commit

Permalink
Make CLockFreeGuard safer
Browse files Browse the repository at this point in the history
  • Loading branch information
prasannavl committed Mar 3, 2023
1 parent 5e3f1c7 commit 40eafe9
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,17 +337,16 @@ class CLockFreeGuard
public:
CLockFreeGuard(std::atomic_bool& lock) : lock(lock)
{
bool desired = false;
while (!lock.compare_exchange_weak(desired, true,
std::memory_order_release,
std::memory_order_relaxed)) {
desired = false;
std::this_thread::sleep_for(std::chrono::milliseconds(1));
bool expected = false;
while (!lock.compare_exchange_weak(expected, true)) {
expected = false;
// std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}

~CLockFreeGuard()
{
lock.store(false, std::memory_order_release);
lock.store(false);
}
};

Expand Down

0 comments on commit 40eafe9

Please sign in to comment.