Fawkes API
Fawkes Development Version
|
Spin lock. More...
#include <>>
Public Member Functions | |
Spinlock () | |
Constructor. | |
~Spinlock () | |
Destructor. | |
void | lock () |
Lock this spinlock. | |
bool | try_lock () |
Tries to lock the spinlock. | |
void | unlock () |
Unlock the spinlock. |
Spin lock.
This class is similar to a Mutex in that it is used in a multi-threading environment to lock access to resources.
The difference is that the spinlock will do a busy waiting until it acquires the lock while the mutex would block and wait, and may even starve if another threads releases a lock only for a short period of time.
Spinlocks are risky, priority inversion may be caused if used improperly. Be sure what you are doing if you use spinlocks.
fawkes::Spinlock::Spinlock | ( | ) |
Constructor.
Definition at line 73 of file spinlock.cpp.
fawkes::Spinlock::~Spinlock | ( | ) |
Destructor.
Definition at line 82 of file spinlock.cpp.
void fawkes::Spinlock::lock | ( | ) |
Lock this spinlock.
A call to lock() will block until the lock on the spinlock could be aquired. If you want to avoid see consider using try_lock().
Definition at line 97 of file spinlock.cpp.
References fawkes::Thread::current_thread().
bool fawkes::Spinlock::try_lock | ( | ) |
Tries to lock the spinlock.
This can also be used to check if a spinlock is locked. The code for this can be:
bool locked = false; if ( spinlock->try_lock() ) { spinlock->unlock(); locked = true; }
This cannot be implemented in Spinlock in a locked() method since this would lead to race conditions in many situations.
Definition at line 131 of file spinlock.cpp.
void fawkes::Spinlock::unlock | ( | ) |
Unlock the spinlock.
Definition at line 147 of file spinlock.cpp.