libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::SpinLock Class Reference

轻量级自旋锁实现 / Lightweight spinlock implementation More...

#include <spin_lock.hpp>

Public Member Functions

bool TryLock () noexcept
 尝试获取锁 / Attempts to acquire the lock
 
void Lock () noexcept
 阻塞直到获取锁 / Blocks until the lock is acquired
 
void Unlock () noexcept
 释放锁 / Releases the lock
 

Private Attributes

std::atomic_flag atomic_flag_ = ATOMIC_FLAG_INIT
 自旋锁标志 / Spinlock flag
 

Detailed Description

轻量级自旋锁实现 / Lightweight spinlock implementation

该类实现了自旋锁 (SpinLock),适用于短时间的锁定操作,避免线程切换的开销。 This class implements a spinlock, which is suitable for short-term locking operations to avoid the overhead of thread context switching.

Definition at line 16 of file spin_lock.hpp.

Member Function Documentation

◆ Lock()

void LibXR::SpinLock::Lock ( )
inlinenoexcept

阻塞直到获取锁 / Blocks until the lock is acquired

该方法会不断轮询,直到成功获取锁,适用于临界区很短的场景。 This method continuously spins until it successfully acquires the lock, making it suitable for scenarios with very short critical sections.

Definition at line 44 of file spin_lock.hpp.

45 {
46 while (atomic_flag_.test_and_set(std::memory_order_acquire))
47 {
48 }
49 }
std::atomic_flag atomic_flag_
自旋锁标志 / Spinlock flag
Definition spin_lock.hpp:19

◆ TryLock()

bool LibXR::SpinLock::TryLock ( )
inlinenoexcept

尝试获取锁 / Attempts to acquire the lock

Returns
如果获取成功返回 true,否则返回 false / Returns true if the lock is acquired successfully, otherwise returns false

该方法不会阻塞当前线程,而是立即尝试获取锁。如果锁已被占用,则返回 false。 This method does not block the current thread but instead attempts to acquire the lock immediately. If the lock is already held, it returns false.

Definition at line 32 of file spin_lock.hpp.

33 {
34 return !atomic_flag_.test_and_set(std::memory_order_acquire);
35 }

◆ Unlock()

void LibXR::SpinLock::Unlock ( )
inlinenoexcept

释放锁 / Releases the lock

该方法清除锁标志,使其他线程可以获取锁。 This method clears the lock flag, allowing other threads to acquire the lock.

Definition at line 57 of file spin_lock.hpp.

57{ atomic_flag_.clear(std::memory_order_release); }

Field Documentation

◆ atomic_flag_

std::atomic_flag LibXR::SpinLock::atomic_flag_ = ATOMIC_FLAG_INIT
private

自旋锁标志 / Spinlock flag

Definition at line 19 of file spin_lock.hpp.


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