1 Star 0 Fork 0

windsPx/lunzi

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
TicketSpin.hpp 916 Bytes
一键复制 编辑 原始数据 按行查看 历史
windsPx 提交于 2023-04-26 11:23 . add TicketSpin.hpp.
#pragma once
#include <atomic>
#define _TICKET_SPIN
name px
{
class _TICKET_SPIN CSpinLock
{
#if 0
union Ticket
{
unsigned int used = 0;
struct
{
unsigned short _tickets;
unsigned short _users;
};
};
Ticket m_ticket;
#endif
std::atomic<unsigned short> m_users;
unsigned short m_ticket;
public:
CSpinLock() : m_users(0), m_ticket(0) {}
CSpinLock(const CSpinLock&) = delete;
CSpinLock& operator=(const CSpinLock&) = delete;
public:
void lock()
{
unsigned short _this = m_users.fetch_add(1, std::memory_order_acquire);
while (_this != m_ticket)
{
std::atomic_thread_fence(std::memory_order_acquire);
}
}
void unlock()
{
++m_ticket;
std::atomic_thread_fence(std::memory_order_release);
}
};
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/windpx/lunzi.git
[email protected]:windpx/lunzi.git
windpx
lunzi
lunzi
master

搜索帮助