{"id":2571,"date":"2018-12-21T11:21:14","date_gmt":"2018-12-21T03:21:14","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=2571"},"modified":"2018-12-21T11:21:14","modified_gmt":"2018-12-21T03:21:14","slug":"c11%e5%8d%95%e5%ae%9e%e4%be%8bsingleton%e5%88%9d%e5%a7%8b%e5%8c%96%e7%9a%84%e5%87%a0%e7%a7%8d%e6%96%b9%e6%b3%95memory-fenceatomiccall_once","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/2571","title":{"rendered":"c++11\u5355\u5b9e\u4f8b(singleton)\u521d\u59cb\u5316\u7684\u51e0\u79cd\u65b9\u6cd5(memory fence,atomic,call_once)"},"content":{"rendered":"<div class=\"p-text\">\u5355\u5b9e\u4f8b\u6a21\u5f0f(singleton)\u4e0b\u8981\u6c42\u4e00\u4e2a\u7c7b\u53ea\u80fd\u6709\u4e00\u4e2a\u5b9e\u4f8b,\u5982\u4f55\u4fdd\u8bc1\u53ea\u521b\u5efa\u4e00\u4e2a\u5b9e\u4f8b\uff1f\u7c7b\u7684\u9759\u6001\u6210\u5458\u5ef6\u8fdf\u521d\u59cb\u5316\u8981\u6c42\u9759\u6001\u6210\u5458\u53ea\u80fd\u88ab\u521d\u59cb\u5316\u4e00\u6b21\uff0c\u4e5f\u6709\u7c7b\u4f3c\u7684\u95ee\u9898\u3002<br \/>\n\u5728\u5355\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0c\u8fd9\u4e8b\u513f\u5f88\u597d\u529e\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Singleton* Singleton::getInstance() {\r\n    if (m_instance == nullptr) {\r\n        m_instance = new Singleton;\r\n    }\r\n}<\/pre>\n<\/div>\n<div><\/div>\n<div class=\"p-text\">\u4f46\u662f\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0c\u4e0a\u9762\u7684\u65b9\u5f0f\u663e\u7136\u662f\u4e0d\u5b89\u5168\u7684\uff0c\u6709\u53ef\u80fd\u9020\u6210\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u521b\u5efa\u591a\u4e2a\u4e0d\u540c\u5b9e\u4f8b\uff0c\u5e76\u4e14\u9664\u4e86\u6700\u540e\u4e00\u4e2a\u5b9e\u4f8b\u88ab\u5f15\u7528\uff0c\u5176\u4ed6\u5b9e\u4f8b\u90fd\u88ab\u4e22\u5f03\u800c\u5f15\u8d77\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<h2>scope-based lock<\/h2>\n<p>\u6240\u4ee5\u5982\u679c\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u5b89\u5168\u4f7f\u7528\u5355\u5b9e\u4f8b\u5bf9\u8c61\uff08Singleton\uff09\uff0c\u6700\u7b80\u5355\u7684\u505a\u6cd5\u662f\u5728\u8bbf\u95ee\u65f6\u5bf9\u51fd\u6570\u52a0\u9501\uff0c\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5047\u5b9a\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8c03\u7528Singleton::getInstance\u65b9\u6cd5\uff0c\u7b2c\u4e00\u4e2a\u83b7\u5f97\u9501\u7684\u7ebf\u7a0b\u8d1f\u8d23\u521b\u5efa\u5b9e\u4f8b,\u5176\u4ed6\u7ebf\u7a0b\u5219\u76f4\u63a5\u8fd4\u56de\u5df2\u7ecf\u521b\u5efa\u7684\u5b9e\u4f8b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Singleton* Singleton::getInstance() {\r\n\/\/ lock\u662f\u57fa\u4e8e\u4f5c\u7528\u57df\u7684\u9501(scope-based lock),\u4f5c\u7528\u57df\u7ed3\u675f\u81ea\u52a8\u91ca\u653e\uff0c\u76f8\u5f53\u4e8ejava\u4e2d\u7684synchronized\u5173\u952e\u5b57\u8d77\u5230\u7684\u4f5c\u7528\uff0c\r\n\/\/\u672c\u4f8b\u4e2d\uff0c\u51fd\u6570\u8fd4\u56de\u65f6\u4f5c\u7528\u57df\u7ed3\u675f\uff0c\u76f8\u5f53\u4e8e\u5bf9\u51fd\u6570\u52a0\u9501\uff0cboost\u4e2dscope_lock\u7c7b\u5b9e\u73b0\u6b64\u529f\u80fd\r\n    Lock lock;  \r\n    if (m_instance == nullptr) {\r\n        m_instance = new Singleton;\r\n    }\r\n    return m_instance;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u65e0\u7591\u662f\u5b89\u5168\u7684\uff0c\u4f46\u662f\u5f53\u5b9e\u4f8b\u88ab\u521b\u5efa\u4e4b\u540e\uff0c\u5b9e\u9645\u4e0a\u5df2\u7ecf\u4e0d\u9700\u8981\u518d\u5bf9\u5176\u8fdb\u884c\u52a0\u9501\uff0c\u52a0\u9501\u867d\u7136\u4e0d\u4e00\u5b9a\u5bfc\u81f4\u6027\u80fd\u4f4e\u4e0b\uff0c\u4f46\u662f\u5728\u91cd\u8d1f\u8f7d\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4e5f\u53ef\u80fd\u5bfc\u81f4\u54cd\u5e94\u7f13\u6162\u3002\u6240\u4ee5\u5bf9\u4e8e\u8ffd\u6c42\u5b8c\u7f8e\u7684\u4eba\u6765\u8bf4\uff0c\u8fd9\u4e2a\u529e\u6cd5\u786e\u5b9e\u6709\u4e9b\u8ba9\u4eba\u4e0d\u723d\u554a\u3002<\/p>\n<h2>\u53cc\u91cd\u68c0\u67e5\u9501\u5b9a\u6a21\u5f0f(DCLP)<\/h2>\n<p>\u4e3a\u4e86\u89e3\u51b3\u4e0a\u9762\u5355\u5b9e\u4f8b\u521d\u59cb\u5316\u591a\u6b21\u52a0\u9501\u7684\u95ee\u9898\uff0c\u7a0b\u5e8f\u5458\u4eec\u60f3\u51fa\u4e86\u53cc\u91cd\u68c0\u67e5\u9501\u5b9a\u6a21\u5f0f(DCLP),\u4f30\u8ba1\u4f60\u4e5f\u60f3\u5230\u4e86\u8fd9\u4e2a\u529e\u6cd5\u3002<br \/>\n\u4ee3\u7801\u539f\u578b\u5982\u4e0b\uff1a<\/p><\/div>\n<div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Singleton* Singleton::getInstance() {\r\n    if (pInstance == nullptr) { \r\n        \/\/ \u7b2c\u4e00\u6b21\u68c0\u67e5\r\n        Lock lock;\r\n        if (pInstance == nullptr) { \r\n            \/\/  \u7b2c\u4e8c\u6b21\u68c0\u67e5\r\n            pInstance = new Singleton;\r\n        }\r\n    }\r\n    return pInstance;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<div class=\"p-text\">\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u7b2c\u4e00\u6b21\u68c0\u67e5\u5e76\u6ca1\u6709\u52a0\u9501\uff0c\u5c31\u907f\u514d\u4e86\u6bcf\u6b21\u8c03\u7528getInstance\u65f6\u90fd\u8981\u52a0\u9501\u7684\u95ee\u9898\u3002\u8c8c\u4f3c\u8fd9\u4e2a\u65b9\u6cd5\u5f88\u5b8c\u7f8e\u4e86\u5427\uff0c\u903b\u8f91\u4e0a\u65e0\u61c8\u53ef\u51fb\uff0c\u4f46\u662f\u6df1\u5165\u7814\u7a76\u5c31\u4f1a\u53d1\u73b0DCLP\u4e5f\u5e76\u4e0d\u662f\u53ef\u9760\u7684\u3002\u5177\u4f53\u7684\u539f\u56e0\u53c2\u89c1\u6b64\u4e0b\u6587\uff0c\u8bf4\u5f97\u5f88\u8be6\u7ec6\u4e86<br \/>\n<a href=\"http:\/\/blog.jobbole.com\/86392\/\" target=\"_blank\" rel=\"noopener\">C++\u548c\u53cc\u91cd\u68c0\u67e5\u9501\u5b9a\u6a21\u5f0f(DCLP)\u7684\u98ce\u9669<\/a><br \/>\n\u8bfb\u8fc7\u4e0a\u9762\u8fd9\u7bc7\u6587\u7ae0\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4e00\u4e2a\u7ed3\u8bba:\u56e0\u4e3ac++\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u8fc7\u7a0b\u4e2d\u4f1a\u5bf9\u4ee3\u7801\u8fdb\u884c\u4f18\u5316\uff0c\u6240\u4ee5\u5b9e\u9645\u7684\u4ee3\u7801\u6267\u884c\u987a\u5e8f\u53ef\u80fd\u88ab\u6253\u4e71\uff0c\u53e6\u5916\u56e0\u4e3aCPU\u6709\u4e00\u7ea7\u4e8c\u7ea7\u7f13\u5b58(cache),CPU\u7684\u8ba1\u7b97\u7ed3\u679c\u5e76\u4e0d\u662f\u53ca\u65f6\u66f4\u65b0\u5230\u5185\u5b58\u7684,\u6240\u4ee5\u5728\u591a\u7ebf\u7a0b\u73af\u5883\uff0c\u4e0d\u540c\u7ebf\u7a0b\u95f4\u5171\u4eab\u5185\u5b58\u6570\u636e\u5b58\u5728\u53ef\u89c1\u6027\u95ee\u9898\uff0c\u4ece\u800c\u5bfc\u81f4\u4f7f\u7528DCLP\u4e5f\u5b58\u5728\u98ce\u9669\u3002<br \/>\n\u5173\u4e8e\u591a\u7ebf\u7a0b\u95f4\u7684\u6570\u636e\u53ef\u89c1\u6027\uff0c\u5c31\u8981\u6d89\u53ca\u5230c++\u7684\u5185\u5b58\u6a21\u578b(memory model)\u7684\u8bdd\u9898\uff0c\u8fd9\u4e2a\u4e8b\u5427\u8fd8\u771f\u4e0d\u592a\u5bb9\u6613\u8bf4\u660e\u767d\uff0c\u63a8\u8350\u4e00\u7bc7\u6bd4\u8f83\u6d45\u663e\u6613\u61c2\u7684\u6587\u7ae0<br \/>\n<a href=\"http:\/\/blog.csdn.net\/cszhouwei\/article\/details\/11730559\" target=\"_blank\" rel=\"noopener\">\u6f2b\u8c08C++11\u591a\u7ebf\u7a0b\u5185\u5b58\u6a21\u578b<\/a><\/p>\n<h2>memory fence\/barrier<\/h2>\n<p>\u5728\u4e0a\u8282\uff0c\u6211\u4eec\u77e5\u9053\u7684\u53cc\u91cd\u68c0\u67e5\u9501\u5b9a\u6a21\u5f0f\u5b58\u5728\u98ce\u9669\uff0c\u90a3\u4e48\u6709\u6ca1\u6709\u529e\u6cd5\u6539\u8fdb\u5462\uff1f<br \/>\n\u529e\u6cd5\u662f\u6709\uff0c\u8fd9\u5c31\u662f\u5185\u5b58\u6805\u680f\u6280\u672f(memory fence),\u4e5f\u79f0\u5185\u5b58\u6805\u969c(memory barrier)<br \/>\n\u5185\u5b58\u6805\u680f\u7684\u4f5c\u7528\u5728\u4e8e\u4fdd\u8bc1\u5185\u5b58\u64cd\u4f5c\u7684\u76f8\u5bf9\u987a\u5e8f\uff0c \u4f46\u5e76\u4e0d\u4fdd\u8bc1\u5185\u5b58\u64cd\u4f5c\u7684\u4e25\u683c\u65f6\u5e8f\uff0c \u786e\u4fdd\u7b2c\u4e00\u4e2a\u7ebf\u7a0b\u66f4\u65b0\u7684\u6570\u636e\u5bf9\u5176\u4ed6\u7ebf\u7a0b\u53ef\u89c1\u3002<br \/>\n\u4e00\u4e2a memory fence\u4e4b\u524d\u7684\u5185\u5b58\u8bbf\u95ee\u64cd\u4f5c\u5fc5\u5b9a\u5148\u4e8e\u5176\u4e4b\u540e\u7684\u5b8c\u6210<br \/>\n\u5173\u4e8e\u5185\u5b58\u6805\u680f\u7684\u8be6\u7ec6\u6982\u5ff5\u53c2\u89c1\uff1a<br \/>\n<a href=\"http:\/\/name5566.com\/4535.html\" target=\"_blank\" rel=\"noopener\">\u7406\u89e3 Memory barrier\uff08\u5185\u5b58\u5c4f\u969c\uff09<\/a><br \/>\n\u4ee5\u4e0b\u662f\u4f7f\u7528\u5185\u5b58\u6805\u680f\u6280\u672f\u6765\u5b9e\u73b0DCLP\u7684\u4f2a\u4ee3\u7801<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Singleton* Singleton::getInstance() {\r\n    Singleton* tmp = m_instance;\r\n    ...                     \r\n\/\/ \u63d2\u5165\u5185\u5b58\u6805\u680f\u6307\u4ee4\r\n    if (tmp == nullptr) {\r\n        Lock lock;\r\n        tmp = m_instance;\r\n        if (tmp == nullptr) {\r\n            tmp = new Singleton; \/\/ \u8bed\u53e51\r\n            ...             \r\n\/\/ \u63d2\u5165\u5185\u5b58\u6805\u680f\u6307\u4ee4,\u786e\u4fdd\u8bed\u53e52\u6267\u884c\u65f6\uff0ctmp\u6307\u5411\u7684\u5bf9\u8c61\u5df2\u7ecf\u5b8c\u6210\u521d\u59cb\u5316\u6784\u9020\u51fd\u6570\r\n            m_instance = tmp;\/\/\u8bed\u53e52            \r\n        }\r\n    }\r\n    return tmp;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u8fd9\u91cc\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff1a\u5728m_instance\u6307\u9488\u4e3aNULL\u65f6\uff0c\u6211\u4eec\u505a\u4e86\u4e00\u6b21\u9501\u5b9a\uff0c\u8fd9\u4e2a\u9501\u5b9a\u786e\u4fdd\u521b\u5efa\u8be5\u5bf9\u8c61\u7684\u7ebf\u7a0b\u5bf9m_instance \u7684\u64cd\u4f5c\u5bf9\u5176\u4ed6\u7ebf\u7a0b\u53ef\u89c1\u3002\u5728\u521b\u5efa\u7ebf\u7a0b\u5185\u90e8\u6784\u9020\u5757\u4e2d\uff0cm_instance\u88ab\u518d\u4e00\u6b21\u68c0\u67e5\uff0c\u4ee5\u786e\u4fdd\u8be5\u7ebf\u7a0b\u4ec5\u521b\u5efa\u4e86\u4e00\u4efd\u5bf9\u8c61\u526f\u672c\u3002<\/p>\n<h2>atomic_thread_fence<\/h2>\n<p>\u5173\u4e8ememory fence\uff0c\u4e0d\u540c\u7684CPU\uff0c\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u6709\u4e0d\u540c\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u8981\u662f\u76f4\u63a5\u4f7f\u7528\u8fd8\u771f\u662f\u9ebb\u70e6\uff0c\u4e0d\u8fc7\uff0cc++11\u4e2d\u5bf9\u8fd9\u4e00\u6982\u5ff5\u8fdb\u884c\u4e86\u62bd\u8c61\uff0c\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u4f7f\u7528\u65b9\u5f0f<br \/>\n\u5728c++11\u4e2d\uff0c\u53ef\u4ee5\u83b7\u53d6(acquire\/consume)\u548c\u91ca\u653e(release)\u5185\u5b58\u6805\u680f\u6765\u5b9e\u73b0\u4e0a\u8ff0\u529f\u80fd\u3002\u4f7f\u7528c++11\u4e2d\u7684atomic\u7c7b\u578b\u6765\u5305\u88c5m_instance\u6307\u9488\uff0c\u8fd9\u4f7f\u5f97\u5bf9m_instance\u7684\u64cd\u4f5c\u662f\u4e00\u4e2a\u539f\u5b50\u64cd\u4f5c\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u5185\u5b58\u6805\u680f\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">std::atomic&lt;Singleton*&gt; Singleton::m_instance;\r\nstd::mutex Singleton::m_mutex;\r\n\r\nSingleton* Singleton::getInstance() {\r\n    Singleton* tmp = m_instance.load(std::memory_order_relaxed);\r\n    std::atomic_thread_fence(std::memory_order_acquire);  \r\n    if (tmp == nullptr) {\r\n        std::lock_guard&lt;std::mutex&gt; lock(m_mutex);\r\n        tmp = m_instance.load(std::memory_order_relaxed);\r\n        if (tmp == nullptr) {\r\n            tmp = new Singleton;\r\n            std::atomic_thread_fence(std::memory_order_release); \r\n            m_instance.store(tmp, std::memory_order_relaxed);\r\n        }\r\n    }\r\n    return tmp;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d<\/p>\n<pre>atomic_thread_fence<\/pre>\n<p>\u5728\u521b\u5efa\u5bf9\u8c61\u7ebf\u7a0b\u548c\u4f7f\u7528\u5bf9\u8c61\u7ebf\u7a0b\u4e4b\u95f4\u5efa\u7acb\u4e86\u4e00\u79cd\u201c\u540c\u6b65-\u4e0e\u201d\u7684\u5173\u7cfb\uff08synchronizes-with\uff09\u3002<br \/>\n\u4ee5\u4e0b\u662f\u6458\u81ea<a href=\"http:\/\/www.cplusplus.com\/reference\/atomic\/atomic_thread_fence\/\" target=\"_blank\" rel=\"noopener\">cplusplus<\/a>\u5173\u4e8eatomic_thread_fence\u51fd\u6570\u7684\u8bf4\u660e:<br \/>\nEstablishes a multi-thread fence: The point of call to this function becomes either an acquire or a release synchronization point (or both).<br \/>\nAll visible side effects from the releasing thread that happen before the call to this function are synchronized to also happen before the call this function in the acquiring thread.<br \/>\nCalling this function has the same effects as a load or store atomic operation, but without involving an atomic value<br \/>\n\u4e2d\u6587\u5927\u610f\u662f\u521b\u5efa\u4e00\u4e2a\u591a\u7ebf\u7a0b\u6805\u680f,\u8c03\u7528\u8be5\u51fd\u6570\u7684\u4f4d\u7f6e\u6210\u4e3a\u4e00\u4e2a(acquire\u6216release\u6216\u4e24\u8005)\u7684\u540c\u6b65\u70b9,<br \/>\n\u5728release\u7ebf\u7a0b\u4e2d\u6b64\u540c\u6b65\u70b9\u4e4b\u524d\u7684\u6570\u636e\u66f4\u65b0\u90fd\u5c06\u540c\u6b65\u4e8eacquire \u7ebf\u7a0b\u7684\u540c\u6b65\u70b9\u4e4b\u524d\uff0c\u8fd9\u5c31\u5b9e\u73b0\u7ebf\u7a0b\u53ef\u89c1\u6027\u4e00\u81f4<\/p>\n<h2>atomic<\/h2>\n<p>\u4e0a\u8282\u7684\u4ee3\u7801\u4f7f\u7528\u5185\u5b58\u6805\u680f\u9501\u5b9a\u6280\u672f\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5b9e\u73b0\u53cc\u91cd\u68c0\u67e5\u9501\u5b9a\u3002\u4f46\u662f\u770b\u7740\u5b9e\u5728\u6709\u70b9\u9ebb\u70e6\uff0c\u5728C++11\u4e2d\u66f4\u597d\u7684\u5b9e\u73b0\u65b9\u5f0f\u662f\u76f4\u63a5\u4f7f\u7528\u539f\u5b50\u64cd\u4f5c\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">std::atomic&lt;Singleton*&gt; Singleton::m_instance;\r\nstd::mutex Singleton::m_mutex;\r\n\r\nSingleton* Singleton::getInstance() {\r\n    Singleton* tmp = m_instance.load(std::memory_order_acquire);\r\n    if (tmp == nullptr) {\r\n        std::lock_guard&lt;std::mutex&gt; lock(m_mutex);\r\n        tmp = m_instance.load(std::memory_order_relaxed);\r\n        if (tmp == nullptr) {\r\n            tmp = new Singleton;\r\n            m_instance.store(tmp, std::memory_order_release);\r\n        }\r\n    }\r\n    return tmp;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u5982\u679c\u4f60\u5bf9<\/p>\n<pre>memory_order<\/pre>\n<p>\u7684\u6982\u5ff5\u8fd8\u662f\u4e0d\u592a\u6e05\u695a\uff0c\u90a3\u4e48\u5c31\u4f7f\u7528C++\u987a\u5e8f\u4e00\u81f4\u7684\u539f\u5b50\u64cd\u4f5c\uff0c\u6240\u6709<\/p>\n<pre>std::atomic<\/pre>\n<p>\u7684\u64cd\u4f5c\u5982\u679c\u4e0d\u5e26\u53c2\u6570\u9ed8\u8ba4\u90fd\u662f<\/p>\n<pre>std::memory_order_seq_cst<\/pre>\n<p>,\u5373\u987a\u5e8f\u7684\u539f\u5b50\u64cd\u4f5c\uff08sequentially consistent\uff09,\u7b80\u79f0SC,\u4f7f\u7528\uff08SC\uff09\u539f\u5b50\u64cd\u4f5c\u5e93\uff0c\u6574\u4e2a\u51fd\u6570\u6267\u884c\u6307\u4ee4\u90fd\u5c06\u4fdd\u8bc1\u987a\u5e8f\u6267\u884c,\u8fd9\u662f\u4e00\u79cd\u6700\u4fdd\u5b88\u7684\u5185\u5b58\u6a21\u578b\u7b56\u7565\u3002<br \/>\n\u4e0b\u9762\u7684\u4ee3\u7801\u5c31\u662f\u4f7f\u7528SC\u539f\u5b50\u64cd\u4f5c\u5b9e\u73b0\u53cc\u91cd\u68c0\u67e5\u9501\u5b9a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">std::atomic&lt;Singleton*&gt; Singleton::m_instance;\r\nstd::mutex Singleton::m_mutex;\r\n\r\nSingleton* Singleton::getInstance() {\r\n    Singleton* tmp = m_instance.load();\r\n    if (tmp == nullptr) {\r\n        std::lock_guard&lt;std::mutex&gt; lock(m_mutex);\r\n        tmp = m_instance.load();\r\n        if (tmp == nullptr) {\r\n            tmp = new Singleton;\r\n            m_instance.store(tmp);\r\n        }\r\n    }\r\n    return tmp;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h2>call_once(\u6700\u7b80\u5355\u7684\u5b9e\u73b0)<\/h2>\n<p>\u524d\u9762\u8bb2\u4e86\u8fa3\u4e48\u591a\u5c31\u4e3a\u4e86\u4e00\u4e2a\u5355\u5b9e\u4f8b\u521d\u59cb\u5316\uff0c\u592a\u590d\u6742\u5566\uff0c\u8bf4\u5b9e\u8bdd\u6211\u770b\u660e\u767d\u4e0a\u9762\u7684\u8fd9\u5185\u5bb9\u4e5f\u82b1\u4e86\u51e0\u5929\u65f6\u95f4\u8865\u5145\u5404\u79cd\u77e5\u8bc6\uff0c\u89c9\u5f97\u603b\u7b97\u641e\u660e\u767d\u4e86\uff0c\u53ef\u4ee5\u6d88\u505c\u4e00\u4e0b\u4e86\uff0c\u4f46\u662f\u6211\u8111\u5b50\u91cc\u7a81\u7136\u4e86\u95ea\u8fc7\u4e00\u4e2a\u540d\u5b57\uff0c\u201dcall_once\u201d\u3002\u3002\u3002<br \/>\n\u8fd9\u662f\u524d\u9635\u5b50\u7ffbc++11\u6807\u51c6\u5934\u6587\u4ef6\u300amutex\u300b\u65f6\u770b\u5230\u7684\u4e00\u4e2a\u51fd\u6570,\u4e8e\u662f\u8d76\u7d27\u53bb\u67e5\u8d44\u6599,<br \/>\n\u4ee5\u4e0b\u662f\u5bf9<\/p>\n<pre>std::call_once<\/pre>\n<p>\u7684\u539f\u6587\u8bf4\u660e\uff1a<br \/>\nfrom:<a href=\"http:\/\/www.cplusplus.com\/reference\/mutex\/call_once\/\" target=\"_blank\" rel=\"noopener\">std::call_once@cplusplus.com<\/a><br \/>\nCalls fn passing args as arguments, unless another thread has already executed (or is currently executing) a call to call_once with the same flag.<br \/>\nIf another thread is already actively executing a call to call_once with the same flag, it causes a passive execution: Passive executions do not call fn but do not return until the active execution itself has returned, and all visible side effects are synchronized at that point among all concurrent calls to this function with the same flag.<br \/>\nIf an active call to call_once ends by throwing an exception (which is propagated to its calling thread) and passive executions exist, one is selected among these passive executions, and called to be the new active call instead.<br \/>\nNote that once an active execution has returned, all current passive executions and future calls to call_once (with the same flag) also return without becoming active executions.<br \/>\nThe active execution uses decay copies of the lvalue or rvalue references of fn and args, ignoring the value returned by fn.<br \/>\nalso see:<br \/>\n<a href=\"https:\/\/msdn.microsoft.com\/zh-cn\/library\/hh921437.aspx\" target=\"_blank\" rel=\"noopener\">call_once \u51fd\u6570 @microsoft<\/a><br \/>\n<a href=\"http:\/\/en.cppreference.com\/w\/cpp\/thread\/call_once\" target=\"_blank\" rel=\"noopener\">std:call_once@cppreference.com<\/a><br \/>\n\u5927\u610f\u5c31\u662f<br \/>\ncall_one\u4fdd\u8bc1\u51fd\u6570fn\u53ea\u88ab\u6267\u884c\u4e00\u6b21\uff0c\u5982\u679c\u6709\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u6267\u884c\u51fd\u6570fn\u8c03\u7528\uff0c\u5219\u53ea\u6709\u4e00\u4e2a\u6d3b\u52a8\u7ebf\u7a0b(active call)\u4f1a\u6267\u884c\u51fd\u6570\uff0c\u5176\u4ed6\u7684\u7ebf\u7a0b\u5728\u8fd9\u4e2a\u7ebf\u7a0b\u6267\u884c\u8fd4\u56de\u4e4b\u524d\u4f1a\u5904\u4e8e\u201dpassive execution\u201d(\u88ab\u52a8\u6267\u884c\u72b6\u6001)\u2014\u4e0d\u4f1a\u76f4\u63a5\u8fd4\u56de\uff0c\u76f4\u5230\u6d3b\u52a8\u7ebf\u7a0b\u5bf9fn\u8c03\u7528\u7ed3\u675f\u624d\u8fd4\u56de\u3002\u5bf9\u4e8e\u6240\u6709\u8c03\u7528\u51fd\u6570fn\u7684\u5e76\u53d1\u7ebf\u7a0b\u7684\u6570\u636e\u53ef\u89c1\u6027\u90fd\u662f\u540c\u6b65\u7684(\u4e00\u81f4\u7684)\u3002<br \/>\n\u5982\u679c\u6d3b\u52a8\u7ebf\u7a0b\u5728\u6267\u884cfn\u65f6\u629b\u51fa\u5f02\u5e38\uff0c\u5219\u4f1a\u4ece\u5904\u4e8e\u201dpassive execution\u201d\u72b6\u6001\u7684\u7ebf\u7a0b\u4e2d\u6311\u4e00\u4e2a\u7ebf\u7a0b\u6210\u4e3a\u6d3b\u52a8\u7ebf\u7a0b\u7ee7\u7eed\u6267\u884cfn,\u4f9d\u6b64\u7c7b\u63a8\u3002<br \/>\n\u4e00\u65e6\u6d3b\u52a8\u7ebf\u7a0b\u8fd4\u56de,\u6240\u6709\u201dpassive execution\u201d\u72b6\u6001\u7684\u7ebf\u7a0b\u4e5f\u8fd4\u56de,\u4e0d\u4f1a\u6210\u4e3a\u6d3b\u52a8\u7ebf\u7a0b\u3002<br \/>\n\u7531\u4e0a\u9762\u7684\u8bf4\u660e\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fe1<\/p>\n<pre>call_once<\/pre>\n<p>\u5b8c\u5168\u6ee1\u8db3\u5bf9\u591a\u7ebf\u7a0b\u72b6\u6001\u4e0b\u5bf9\u6570\u636e\u53ef\u89c1\u6027\u7684\u8981\u6c42\u3002<br \/>\n\u6240\u4ee5\u5229\u7528<\/p>\n<pre>call_once<\/pre>\n<p>\u518d\u7ed3\u5408lambda\u8868\u8fbe\u5f0f,\u524d\u9762\u51e0\u8282\u90a3\u4e48\u591a\u590d\u6742\u4ee3\u7801\uff0c\u5728\u8fd9\u91cc\u5343\u8a00\u4e07\u8bed\u51dd\u805a\u4e3a\u4e00\u53e5\u8bdd\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Singleton* Singleton::m_instance;\r\nSingleton* Singleton::getInstance() {\r\n    static std::once_flag oc;\/\/\u7528\u4e8ecall_once\u7684\u5c40\u90e8\u9759\u6001\u53d8\u91cf\r\n    std::call_once(oc, [&amp;] {  m_instance = new Singleton();});\r\n    return m_instance;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u672c\u6587\u4e2d\u63d0\u5230\u7684\u51e0\u79cd\u65b9\u6cd5\u90fd\u662f\u5b89\u5168\u53ef\u7528\u7684\u65b9\u6848,\u5177\u4f53\u7528\u54ea\u79cd,\u6211\u4e2a\u4eba\u89c9\u5f97\u8fd8\u662fcall_once\u6700\u7b80\u5355\uff0c\u6211\u80af\u5b9a\u9009call_one\u3002\u4f46\u4e0d\u4ee3\u8868\u524d\u9762\u7684\u90a3\u4e48\u591a\u90fd\u767d\u5199\u4e86\uff0c\u5176\u5b9e\u5b66\u4e60\u6bcf\u79cd\u65b9\u6cd5\u8fc7\u7a0b\u4e2d\u8ba9\u6211\u5bf9c++11\u5185\u5b58\u6a21\u578b\u6709\u4e86\u66f4\u6df1\u5165\u7684\u7406\u89e3\uff0c\u8fd9\u624d\u662f\u6700\u5927\u7684\u6536\u83b7\u3002<br \/>\n\u5728\u5199\u672c\u6587\u65f6\u53c2\u8003\u4e86\u4e0b\u9762\u7684\u6587\u7ae0\uff0c\u7279\u5411\u4f5c\u8005\u8868\u793a\u611f\u8c22<br \/>\n<a href=\"http:\/\/blog.csdn.net\/cywosp\/article\/details\/8968655\" target=\"_blank\" rel=\"noopener\">C++11 \u591a\u7ebf\u7a0b\u4e2d\u7684call once<\/a><br \/>\n<a href=\"http:\/\/blog.jobbole.com\/52164\/\" target=\"_blank\" rel=\"noopener\">C++11 \u4fee\u590d\u4e86\u53cc\u91cd\u68c0\u67e5\u9501\u5b9a\u95ee\u9898<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5355\u5b9e\u4f8b\u6a21\u5f0f(singleton)\u4e0b\u8981\u6c42\u4e00<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[119],"class_list":["post-2571","post","type-post","status-publish","format-standard","hentry","tag-03-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/2571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=2571"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/2571\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=2571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=2571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=2571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}