简易的旋转锁
使用 C++11 的原子操作,实现的简易旋转锁(xspinlock.h):
/*** @file xspinlock.h* <pre>* Copyright (c) 2019, Gaaagaa All rights reserved.* * 文件名称:xspinlock.h* 创建日期:2019年01月22日* 文件标识:* 文件摘要:简易的旋转锁类。* * 当前版本:1.0.0.0* 作 者:* 完成日期:2019年01月22日* 版本摘要:* * 历史版本:* 原作者 :* 完成日期:* 版本摘要:* </pre>*/#ifndef __XSPINLOCK_H__
#define __XSPINLOCK_H__#include <atomic>
#include <thread>// x_spinlock_t/*** @class x_spinlock_t* @brief 简易的旋转锁类。*/
class x_spinlock_t
{// constructor/destructor
public:x_spinlock_t(void) { }~x_spinlock_t(void) { }// public interfaces
public:/**********************************************************//*** @brief 加锁操作接口。*/void lock(void){while (m_xspin_flag.test_and_set(std::memory_order_acquire))std::this_thread::yield();}/**********************************************************//*** @brief 解锁操作接口。*/void unlock(void){m_xspin_flag.clear(std::memory_order_release);}// data members
private:std::atomic_flag m_xspin_flag = ATOMIC_FLAG_INIT; ///< 旋转标志
};#endif // __XSPINLOCK_H__