推荐使用局部静态变量实现线程安全单例,C++11保证其初始化线程安全,代码简洁高效;2. 可选std::call_once配合std::once_flag实现精细控制;3. 双重检查锁定虽可行但易出错,不推荐新手使用。

在C++中实现线程安全的单例模式,关键在于确保多个线程同时调用时,实例只被创建一次且不会出现竞争条件。现代C++(C++11及以上)提供了语言级别的保证,让实现变得简单可靠。
代码示例如下:
class Singleton {
public:
    static Singleton& getInstance() {
        static Singleton instance; // 局部静态变量,自动线程安全
        return instance;
    }
<pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;private: Singleton() = default; ~Singleton() = default; };
优点:简洁、高效、无需手动加锁,由编译器保证初始化时的线程安全。
适用场景:绝大多数现代C++项目都可直接使用此方式。
立即学习“C++免费学习笔记(深入)”;
示例代码:
#include <mutex>
<p>class Singleton {
public:
static Singleton& getInstance() {
std::call_once(onceFlag, []() {
instance.reset(new Singleton);
});
return *instance;
}</p><pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;private: Singleton() = default; ~Singleton() = default;
static std::unique_ptr<Singleton> instance; static std::once_flag onceFlag;
};
std::unique_ptr<Singleton> Singleton::instance = nullptr; std::once_flag Singleton::onceFlag;
优点:明确控制初始化时机,适用于复杂构造逻辑。
缺点:代码稍复杂,性能略低于静态变量方式。
示例:
#include <mutex>
#include <atomic>
<p>class Singleton {
public:
static Singleton<em> getInstance() {
Singleton</em> tmp = instance.load();
if (!tmp) {
std::lock<em>guard<std::mutex> lock(mutex</em>);
tmp = instance.load();
if (!tmp) {
tmp = new Singleton();
instance.store(tmp);
}
}
return tmp;
}</p><pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;private: Singleton() = default; ~Singleton() = default;
static std::atomic<Singleton*> instance; static std::mutex mutex_;
};
std::atomic<Singleton*> Singleton::instance{nullptr}; std::mutex Singleton::mutex_;
注意:虽然可行,但容易因内存顺序问题导致未定义行为,建议优先使用前两种方法。
基本上就这些。对于大多数情况,推荐使用局部静态变量方式,它简洁、安全、高效,是现代C++实现线程安全单例的最佳实践。
以上就是c++++怎么实现一个线程安全的单例模式_c++线程安全单例设计实现方法的详细内容,更多请关注php中文网其它相关文章!
 
                        
                        c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
 
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号