UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
ufo::Spinlock Class Reference

A simple spinlock implementation using std::atomic_flag. More...

#include <lib/utility/include/ufo/utility/spinlock.hpp>

Public Member Functions

void lock () noexcept
 Acquires the lock. Spins until the lock is available.
 
bool try_lock () noexcept
 Attempts to acquire the lock without blocking.
 
void unlock () noexcept
 Releases the lock and notifies one waiting thread.
 

Detailed Description

A simple spinlock implementation using std::atomic_flag.

Spinlock is a lightweight mutual exclusion primitive for short critical sections. It repeatedly checks a flag until it can acquire the lock, avoiding thread suspension.

Uses C++20 atomic wait/notify for efficient spinning.

Example usage:

// critical section
lock.unlock();
A simple spinlock implementation using std::atomic_flag.
Definition spinlock.hpp:68
void lock() noexcept
Acquires the lock. Spins until the lock is available.
Definition spinlock.cpp:47

Definition at line 67 of file spinlock.hpp.

Member Function Documentation

◆ lock()

void ufo::Spinlock::lock ( )
noexcept

Acquires the lock. Spins until the lock is available.

Uses atomic wait/notify for efficient spinning.

Author
Daniel Duberg (danie.nosp@m.ldub.nosp@m.erg@g.nosp@m.mail.nosp@m..com)
See also
https://github.com/UnknownFreeOccupied/ufo
Version
1.0
Date
2026-02-22

BSD 3-Clause License

Copyright (c) 2020-2026, Daniel Duberg All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition at line 47 of file spinlock.cpp.

◆ try_lock()

bool ufo::Spinlock::try_lock ( )
noexcept

Attempts to acquire the lock without blocking.

Returns
true if lock was acquired, false otherwise.

Definition at line 54 of file spinlock.cpp.

◆ unlock()

void ufo::Spinlock::unlock ( )
noexcept

Releases the lock and notifies one waiting thread.

Definition at line 59 of file spinlock.cpp.


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