代码拉取完成,页面将自动刷新
#pragma once
#include <atomic>
#include <thread>
#include "IReadWriteLock.h"
class WeakSpinReadWriteLock : public IReadWriteLock {
private:
std::atomic<int> read_cnt;
std::atomic<bool> write_flag;
public:
WeakSpinReadWriteLock() : read_cnt(0), write_flag(false) {
}
void lockRead() override {
while (write_flag.load(std::memory_order_acquire)) {}
read_cnt.fetch_add(1, std::memory_order_release);
}
void unlockRead() override {
read_cnt.fetch_add(-1, std::memory_order_release);
}
void lockWrite(bool yield) override {
while (true) {
int c_read_cnt = read_cnt.load(std::memory_order_acquire);
bool c_write_flag = write_flag.load(std::memory_order_acquire);
if (c_read_cnt || c_write_flag) {
if (yield) std::this_thread::yield();
continue;
}
if (write_flag.compare_exchange_weak(c_write_flag, true, std::memory_order_release)) {
while (read_cnt.load(std::memory_order_acquire) != 0) {}
break;
}
}
}
void lockWrite() override {
lockWrite(false);
}
void unlockWrite() override {
write_flag.store(false, std::memory_order_release);
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。