Fawkes API  Fawkes Development Version
fawkes::Spinlock Class Reference

Spin lock. More...

#include <>>

List of all members.

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.

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

Constructor & Destructor Documentation

fawkes::Spinlock::Spinlock ( )

Constructor.

Definition at line 73 of file spinlock.cpp.

fawkes::Spinlock::~Spinlock ( )

Destructor.

Definition at line 82 of file spinlock.cpp.


Member Function Documentation

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.

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

Definition at line 131 of file spinlock.cpp.

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: