Qt自己就提供了專門的宏 Q_GLOBAL_STATIC。經過這個宏不但定義簡單,還能夠得到線程安全性。html
一、先看官方文檔安全
https://doc.qt.io/qt-5/qglobalstatic.htmlspa
https://doc.qt.io/qt-5/threads-reentrancy.html.net
二、再看使用方法線程
Q_GLOBAL_STATIC(Type, VariableName) Q_GLOBAL_STATIC_WITH_ARGS(Type, VariableName, Arguments)
rule.hdebug
#ifndef RULE_H #define RULE_H class Rule { public: static Rule* instance(); }; #endif // RULE_H
rule.cpp指針
#include "rule.h" Q_GLOBAL_STATIC(Rule, rule) Rule* Rule::instance() { return rule(); }
在任何地方,引用頭文件 include "rule.h"code
就能夠 Rule::instance()->xxxxxx()htm
---blog
參考文獻
https://www.cnblogs.com/findumars/p/10392770.html
http://qtdebug.com/qtbook-singleton/
引伸閱讀
https://doc.qt.io/qt-5/qthreadpool.html Qt線程池
https://doc.qt.io/qt-5/qsharedpointer.html Qt 智能指針
https://doc.qt.io/QtMQTT/index.html Qt Mqtt