gpt4 book ai didi

c++ - call_once 通过双重检查锁定模式实现

转载 作者:行者123 更新时间:2023-11-28 07:27:50 25 4
gpt4 key购买 nike

boost::call_once 是如何实现的?

这个是否使用双重检查锁定模式?

Qt 或 POCO 库中是否有任何等效的实现?

最佳答案

我偶然发现了这个老问题..4 年后,您的案例可能不再有趣,但我相信示例实现可能仍然具有一定的值(value)。

与评论部分声称 DCLP 已损坏相反,这在 C++11 中不再成立,因为它提供了必要的原子类型、操作和内存屏障。

这是一个最小的实现,不一定是 boost 实现。忽略异常行为。
my_call_once 的参数函数保证被调用不超过一次并且共享数据在线程之间正确同步..就像真实的 std::call_once

#include <iostream>
#include <thread>
#include <mutex>
#include <atomic>

class my_once_flag {
std::mutex mtx;
std::atomic<bool> flg{false};

template<typename Func, typename... Args>
friend void my_call_once(my_once_flag&, Func&&, Args&&...);
};

template<typename Func, typename... Args>
void my_call_once(my_once_flag& flag, Func&& f, Args&&... args)
{
if (flag.flg.load(std::memory_order_acquire) == false)
{
std::lock_guard<std::mutex> lck{flag.mtx};

if (flag.flg.load(std::memory_order_relaxed) == true)
return;

std::forward<Func>(f)(std::forward<Args>(args)...);

flag.flg.store(true, std::memory_order_release);
}
}

void just_once(int i) { std::cout << "once " << i << std::endl; }

int main()
{
my_once_flag of;

std::thread {[&]{ my_call_once(of, just_once, 1); }}.join();
std::thread {[&]{ my_call_once(of, just_once, 2); }}.join();
}

关于c++ - call_once 通过双重检查锁定模式实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18389091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com