-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace mutex with tryable spin lock
- Loading branch information
Ubuntu
committed
Aug 22, 2016
1 parent
c7e60c8
commit 639d193
Showing
2 changed files
with
30 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef __TRYABLE_SPIN_LOCK_HPP__ | ||
#define __TRYABLE_SPIN_LOCK_HPP__ | ||
|
||
#include <atomic> | ||
|
||
// Taken from http://stackoverflow.com/questions/26583433/c11-implementation-of-spinlock-using-atomic | ||
class TryableSpinLock { | ||
std::atomic_flag locked = ATOMIC_FLAG_INIT ; | ||
public: | ||
void lock() { | ||
while (locked.test_and_set(std::memory_order_acquire)) { ; } | ||
} | ||
|
||
// from http://stackoverflow.com/questions/19742993/implementing-a-spinlock-in-boost-example-neededhttp://stackoverflow.com/questions/19742993/implementing-a-spinlock-in-boost-example-needed | ||
// is this legit? | ||
bool try_lock() { | ||
return !locked.test_and_set(std::memory_order_acquire); | ||
} | ||
|
||
void unlock() { | ||
locked.clear(std::memory_order_release); | ||
} | ||
}; | ||
|
||
#endif //__TRYABLE_SPIN_LOCK_HPP__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters