C++????????????д???????????
???????????? ???????[ 2015/1/13 13:51:49 ] ????????C++ ????? net
??????????????C++11??????????????????????DCL????????????£?
????1 atomic<Widget*> Widget::pInstance{ nullptr };
????2 Widget* Widget::Instance() {
????3 if (pInstance == nullptr) {
????4 lock_guard<mutex> lock{ mutW };
????5 if (pInstance == nullptr) {
????6 pInstance = new Widget();
????7 }
????8 }
????9 return pInstance;
????10 }
????C++11?е?atomic??????memory_order_seq_cst?????3??6?д?????????????????????atomic????Щ??????????????????????д????????汾??
????1 atomic<Widget*> Widget::pInstance{ nullptr };
????2 Widget* Widget::Instance() {
????3 Widget* p = pInstance;
????4 if (p == nullptr) {
????5 lock_guard<mutex> lock{ mutW };
????6 if ((p = pInstance) == nullptr) {
????7 pInstance = p = new Widget();
????8 }
????9 }
????10 return p;
????11 }
?????????C++????????????????????????????????????????????????????????
????1 static unique_ptr<widget> widget::instance;
????2 static std::once_flag widget::create;
????3 widget& widget::get_instance() {
????4 std::call_once(create?? [=]{ instance = make_unique<widget>(); });
????5 return instance;
????6 }
??????????????????????????????????????????????????????????????????????????C++memory model?ж?static local variable???????The initialization of such a variable is defined to occur the first time control passes through its declaration; for multiple threads calling the function?? this means there’s the potential for a race condition to define first.?????????????????????Ч???????????C++11????
????1 widget& widget::get_instance() {
????2 static widget instance;
????3 return instance;
????4 }
??????Herb Sutter?????????????????“Best of All”???
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11