Fawkes API  Fawkes Development Version
fawkes::Spinlock Class Reference

Spin lock. More...

#include <>>

Public Member Functions

 Spinlock ()
 Constructor. More...
 
 ~Spinlock ()
 Destructor. More...
 
void lock ()
 Lock this spinlock. More...
 
bool try_lock ()
 Tries to lock the spinlock. More...
 
void unlock ()
 Unlock the spinlock. More...
 

Detailed Description

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.

Author
Tim Niemueller

Definition at line 32 of file spinlock.h.

Constructor & Destructor Documentation

◆ Spinlock()

fawkes::Spinlock::Spinlock ( )

Constructor.

Definition at line 73 of file spinlock.cpp.

◆ ~Spinlock()

fawkes::Spinlock::~Spinlock ( )

Destructor.

Definition at line 82 of file spinlock.cpp.

Member Function Documentation

◆ lock()

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().

◆ try_lock()

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.

Returns
true, if the spinlock could be locked, false otherwise.

Definition at line 131 of file spinlock.cpp.

◆ unlock()

void fawkes::Spinlock::unlock ( )

Unlock the spinlock.

Definition at line 147 of file spinlock.cpp.


The documentation for this class was generated from the following files: