C++11 —— 简易的旋转锁类
生活随笔
收集整理的這篇文章主要介紹了
C++11 —— 简易的旋转锁类
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
簡(jiǎn)易的旋轉(zhuǎn)鎖
使用 C++11 的原子操作,實(shí)現(xiàn)的簡(jiǎn)易旋轉(zhuǎn)鎖(xspinlock.h):
/*** @file xspinlock.h* <pre>* Copyright (c) 2019, Gaaagaa All rights reserved.* * 文件名稱(chēng):xspinlock.h* 創(chuàng)建日期:2019年01月22日* 文件標(biāo)識(shí):* 文件摘要:簡(jiǎn)易的旋轉(zhuǎn)鎖類(lèi)。* * 當(dāng)前版本: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 簡(jiǎn)易的旋轉(zhuǎn)鎖類(lèi)。*/ 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; ///< 旋轉(zhuǎn)標(biāo)志 };#endif // __XSPINLOCK_H__轉(zhuǎn)載于:https://www.cnblogs.com/VxGaaagaa/p/10320072.html
總結(jié)
以上是生活随笔為你收集整理的C++11 —— 简易的旋转锁类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: .Net System.Object类介
- 下一篇: 使用devcon禁用启用网卡