单例模式 与 call_once

本文探讨了单例模式,包括懒汉式和饿汉式的实现,特别强调了在懒汉式中不使用互斥锁的错误示范以及如何通过使用互斥锁来修正。此外,还介绍了利用C++的`call_once`特性来实现单例类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

单例模式

单例模式,也叫单子模式,是一种常用的软件设计模式。 在应用这个模式时,单例对象的类必须保证只有一个实例存在。 许多时候整个系统只需要拥有一个的全局对象,这样有利于我们协调系统整体的行为。


懒汉式

不使用互斥(错误示范)

#include <iostream>
#include <thread>

using namespace std;

class Singelton
{
public:
	static Singelton* GetInstance();
	void FreeInstance();

private:
	Singelton(); // 使构造函数私有
	static Singelton* instance_;
};

Singelton* Singelton::instance_ = nullptr;

Singelton::Singelton()
{
	cout << "构造函数:" << this << endl;
}

Singelton* Singelton::GetInstance()
{
    // 没有锁,多线程下可能会多次实例化
    if (instance_ == nullptr)
        instance_ = new Singelton;
	return instance_;
}

void Singelton::FreeInstance()
{
	delete instance_;
	instance_ = nullptr;
}


#pragma region 测试单例类

void thread_func(int id)
{
	Singelton* instance = Singelton::GetInstance();
	cout << "线程" << id << "获得实例:" << instance << endl;
}

int main()
{
	thread t1(thread_func, 1);
	thread t2(thread_func, 2);
	t1.join();
	t2.join();

	cout << "运行结束" << endl;
}

#pragma endregion
Output
构造函数:01325468
线程1获得实例:01325468
构造函数:013316F0
线程2获得实例:013316F0
运行结束

使用互斥

#include <iostream>
#include <mutex>

using namespace std;

mutex singelton_mutex;

class Singelton
{
public:
	static Singelton* GetInstance();
	void FreeInstance();

private:
	Singelton(); // 使构造函数私有
	static Singelton* instance_;
};

Singelton* Singelton::instance_ = nullptr;

Singelton::Singelton()
{
	cout << "构造函数:" << this << endl;
}

Singelton* Singelton::GetInstance()
{
	if (instance_ == nullptr) // 双重锁定
	{
		lock_guard<mutex> lock(singelton_mutex);
		if (instance_ == nullptr)
			instance_ = new Singelton;
	}
	return instance_;
}

void Singelton::FreeInstance()
{
	delete instance_;
	instance_ = nullptr;
}


#pragma region 测试单例类

void thread_func(int id)
{
	Singelton* instance = Singelton::GetInstance();
	cout << "线程" << id << "获得实例:" << instance << endl;
}

int main()
{
	thread t1(thread_func, 1);
	thread t2(thread_func, 2);
	t1.join();
	t2.join();

	cout << "运行结束" << endl;
}

#pragma endregion
Output
构造函数:00CA53D0
线程1获得实例:00CA53D0
线程2获得实例:00CA53D0
运行结束

饿汉式

#include <iostream>
#include <thread>

using namespace std;

class Singelton
{
public:
	static Singelton* GetInstance();

private:
	Singelton(); // 使构造函数私有
	static Singelton* instance_;
};

Singelton* Singelton::instance_ = new Singelton;

Singelton::Singelton()
{
	cout << "构造函数:" << this << endl;
}

Singelton* Singelton::GetInstance()
{
	return instance_;
}


#pragma region 测试单例类

void thread_func(int id)
{
	Singelton* instance = Singelton::GetInstance();
	cout << "线程" << id << "获得实例:" << instance << endl;
}

int main()
{
	thread t1(thread_func, 1);
	thread t2(thread_func, 2);
	t1.join();
	t2.join();

	cout << "运行结束" << endl;
}

#pragma endregion
Output
构造函数:0134A090
线程1 获得实例:0134A090
线程2 获得实例:0134A090
运行结束

call_once 实现单例类

#include <iostream>
#include <mutex>

using namespace std;

std::once_flag singelton_once_flag;

class Singelton
{
public:
	static Singelton* GetInstance();
private:
	Singelton(); // 使构造函数私有
	static void CreateInstance();
	static Singelton* instance_;
};

Singelton* Singelton::instance_ = nullptr;

Singelton::Singelton()
{
	cout << "构造函数:" << this << endl;
}

void Singelton::CreateInstance()
{
	instance_ = new Singelton;
	cout << "创建实例:" << instance_ << endl;
}

Singelton* Singelton::GetInstance()
{
	// CreateInstance 只会执行一次
	std::call_once(singelton_once_flag, Singelton::CreateInstance);
	return instance_;
}


#pragma region 测试单例类

void thread_func(int id)
{
	Singelton* instance = Singelton::GetInstance();
	cout << "线程" << id << "获得实例:" << instance << endl;
}

int main()
{
	thread t1(thread_func, 1);
	thread t2(thread_func, 2);
	t1.join();
	t2.join();

	cout << "运行结束" << endl;
}

#pragma endregion
Output
构造函数:007D5468
创建实例:007D5468
线程2获得实例:007D5468
线程1获得实例:007D5468
运行结束
<think>我们使用Qt框架,但智能指针和call_once是C++11标准库的内容,可以在Qt项目中使用。 目标:使用智能指针(如std::shared_ptr)和std::call_once实现线程安全的单例模式。 步骤: 1. 类:将构造函数、拷贝构造函数、赋值运算符私有化或删除,以防止外部创建实。 2. 使用静态成员变量:一个静态的std::shared_ptr指向对象,以及一个静态的std::once_flag用于保证初始化只执行一次。 3. 提供一个公共的静态成员函数获取对象,在该函数内使用std::call_once来初始化对象。 注意:由于std::call_once是线程安全的,因此可以保证在多线程环境下只初始化一次。 代码示: ```cpp #include <memory> #include <mutex> // 包含std::once_flag和std::call_once class Singleton { public: // 删除拷贝构造函数和赋值运算符,确保唯一性 Singleton(const Singleton&) = delete; Singleton& operator=(const Singleton&) = delete; // 获取对象的静态方法 static std::shared_ptr<Singleton> getInstance() { std::call_once(initInstanceFlag, &Singleton::initSingleton); return instance; } private: Singleton() {} // 私有构造函数 // 静态成员变量 static std::shared_ptr<Singleton> instance; static std::once_flag initInstanceFlag; // 用于初始化对象的函数 static void initSingleton() { instance = std::shared_ptr<Singleton>(new Singleton()); } }; // 静态成员变量需要在类外初始化 std::shared_ptr<Singleton> Singleton::instance = nullptr; std::once_flag Singleton::initInstanceFlag; ``` 但是,上述代码在C++11中,静态局部变量的初始化是线程安全的,所以还有一种更简洁的写法(Meyers' Singleton): 不过这里题目要求使用智能指针和call_once,所以我们按上述方式实现。 然而,我们也可以使用局部静态变量,但这里我们使用智能指针,并且希望使用std::call_once。 但是注意:上述代码中,我们使用了一个静态的std::shared_ptr,并且通过std::call_once来初始化它。 另外,我们也可以使用std::unique_ptr,因为单例模式通常希望只有一个所有者(虽然shared_ptr也可以,但通常不需要共享所有权,因为对象在程序运行期间一直存在,不会被删除)。但是,使用shared_ptr可以方便地支持弱引用,并且当程序需要释放时(比如在程序退出时),可以通过重置shared_ptr来释放。不过,通常在整个程序运行期间都存在,所以这里使用shared_ptr或unique_ptr都可以,但shared_ptr更常见于可能需要提前释放或需要弱指针的场景。 如果我们使用unique_ptr,则修改如下: ```cpp class Singleton { public: Singleton(const Singleton&) = delete; Singleton& operator=(const Singleton&) = delete; static Singleton* getInstance() { std::call_once(initInstanceFlag, &Singleton::initSingleton); return instance.get(); } private: Singleton() {} static std::unique_ptr<Singleton> instance; static std::once_flag initInstanceFlag; static void initSingleton() { instance.reset(new Singleton()); } }; std::unique_ptr<Singleton> Singleton::instance = nullptr; std::once_flag Singleton::initInstanceFlag; ``` 但是,通常我们更倾向于返回引用或原始指针(因为的生命周期由类自己管理,不需要外部管理),而使用智能指针内部管理主要是为了自动释放。不过,在程序退出时,静态成员变量的析构顺序可能导致问题(比如在被析构后,其他静态对象还试图访问它)。使用原始指针则不会自动释放,但我们可以接受,因为对象在程序结束时由系统回收(除非有资源需要释放,如文件句柄、网络连接等)。 因此,为了确保资源释放,我们可以使用智能指针来管理,并在程序退出时自动释放。 但是,如果我们使用shared_ptr,那么当最后一个引用释放时,对象就会被销毁。而单例模式通常希望在整个程序运行期间都存在,所以我们需要保证这个shared_ptr不会被意外释放(比如通过getInstance返回的shared_ptr的拷贝被销毁不会影响,因为静态成员instance还存在)。因此,我们返回shared_ptr的拷贝是安全的。 不过,使用unique_ptr的话,我们只能返回原始指针,因为unique_ptr不允许拷贝。这样,外部就只能通过原始指针访问,而无法控制的生命周期。这符合单例模式的特点。 因此,两种方式都可以,这里我选择使用shared_ptr,因为这样外部也可以持有智能指针,但注意不要重置这个智能指针。 另外,在Qt中,我们可能还需要考虑QObject的父子对象关系,但通常没有父对象。 但是,如果类继承自QObject,则需要注意:QObject不允许拷贝,所以我们的类如果是QObject的派生类,那么拷贝构造函数和赋值运算符已经被删除了,所以不需要再写。另外,QObject的构造函数可以指定父对象,但对象没有父对象,所以构造时不要传递父对象。 下面给出一个继承自QObject的类示(使用shared_ptr): ```cpp #include <QObject> #include <memory> #include <mutex> class Singleton : public QObject { Q_OBJECT public: static std::shared_ptr<Singleton> getInstance() { std::call_once(initInstanceFlag, &Singleton::initSingleton); return instance; } // 删除拷贝构造和赋值 Singleton(const Singleton&) = delete; Singleton& operator=(const Singleton&) = delete; private: Singleton(QObject *parent = nullptr) : QObject(parent) { // 初始化代码 } static std::shared_ptr<Singleton> instance; static std::once_flag initInstanceFlag; static void initSingleton() { instance = std::make_shared<Singleton>(); } }; // 静态成员初始化 std::shared_ptr<Singleton> Singleton::instance = nullptr; std::once_flag Singleton::initInstanceFlag; ``` 注意:在Qt中,如果使用QObject,那么对象树管理可能会影响析构。但这里我们使用智能指针,当程序结束时,静态成员变量会析构,从而释放对象。由于对象没有父对象,所以不会在父对象析构时被删除,而是由智能指针管理。 另外,如果对象在析构时需要执行一些操作(如保存状态),可以在析构函数中实现。 但是,使用std::call_once和智能指针,我们实现了线程安全的单例模式。 相关问题: 1. 为什么使用std::call_once比双重检查锁定更优? 2. 在单例模式中,使用std::shared_ptr和std::unique_ptr有什么区别? 3. 在Qt中使用单例模式需要注意哪些问题? 4. 如何确保对象在程序退出时安全释放资源?</think>### Qt C++ 使用智能指针和 std::call_once 实现线程安全的单例模式 以下是使用 `std::shared_ptr` 和 `std::call_once` 实现线程安全单例模式的完整代码: ```cpp #include <memory> #include <mutex> #include <QCoreApplication> class Singleton { public: // 获取的静态方法 static std::shared_ptr<Singleton> getInstance() { std::call_once(initFlag, &Singleton::init); // 保证只初始化一次 return instance; } // 删除拷贝构造和赋值运算符 Singleton(const Singleton&) = delete; Singleton& operator=(const Singleton&) = delete; // 示成员函数 void doSomething() { qDebug() << "Singleton instance at: " << this; } private: Singleton() = default; // 私有构造函数 ~Singleton() = default; // 私有析构函数 static void init() { // 实际初始化函数 instance.reset(new Singleton()); } static std::shared_ptr<Singleton> instance; // 智能指针管理实 static std::once_flag initFlag; // 初始化控制标志 }; // 静态成员初始化 std::shared_ptr<Singleton> Singleton::instance = nullptr; std::once_flag Singleton::initFlag; int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); // 线程安全访问示 auto threadFunc = []{ auto s = Singleton::getInstance(); s->doSomething(); }; std::thread t1(threadFunc); std::thread t2(threadFunc); t1.join(); t2.join(); return app.exec(); } ``` #### 关键实现说明: 1. **智能指针管理** 使用 `std::shared_ptr` 管理生命周期,自动处理内存释放[^2]。 2. **线程安全初始化** `std::call_once` 配合 `std::once_flag` 确保多线程环境下只执行一次初始化[^1]。 3. **禁用拷贝操作** 显式删除拷贝构造函数和赋值运算符,防止意外复制实。 4. **私有构造函数** 确保只能通过 `getInstance()` 获取实,防止外部创建。 5. **Qt 集成** 可继承 `QObject` 并在构造函数中添加 Qt 相关功能(如信号槽)。 #### 优势: 1. **线程安全**:无需手动加锁,避免死锁风险[^1][^4] 2. **资源高效**:`call_once` 比互斥锁性能更高 3. **自动内存管理**:智能指针自动处理资源释放 4. **延迟初始化**:实在首次调用时创建 > **注意**:如果不需要共享所有权,可将 `std::shared_ptr` 替换为 `std::unique_ptr`,此时 `getInstance()` 需返回原始指针: > ```cpp > static Singleton* getInstance() { > std::call_once(initFlag, []{ instance.reset(new Singleton()); }); > return instance.get(); // 返回原始指针 > } > ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值