[問題] create singleInstance at same time

看板C_and_CPP作者 (不能閒下來!!)時間6年前 (2019/04/06 12:44), 6年前編輯推噓0(006)
留言6則, 3人參與, 6年前最新討論串1/1
開發平台(Platform): (Ex: Win10, Linux, ...) gun c 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): HPe 面試題 有個class 叫foo是 single instance Instance為NULL 如果有兩個thread "同時" Get singleInstance 該怎解決這問題? 餵入的資料(Input): 同一時間init foo class thread1: pfool = foo::getInstance(); thread2: pfool = foo::getInstance(); class foo{ public: static foo *getInstance(){ if( instance == null ) instance = new foo(); return instance; } private: foo(){ } static foo* instance; std::mutex mutex; } 預期的正確結果(Expected Output): 只會有一個pf位址 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔) 補充說明(Supplement): 我記得曾經看過一篇文章getInstance mutex還未初始化所以不能用 -- ▁▁ Google 女友|██████████████████▕搜尋進階搜尋 | 使用偏好 ▇▇  ̄ ̄ ̄ ̄  ̄ ̄ ̄ ̄ 搜尋: ⊙所有網頁 ○中文網頁○繁體中文網頁 ○台灣的網頁 所有網頁 約有0項符合女友的查詢結果,以下是第 0項。 共費30年 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.169.137.208 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1554525869.A.C59.html

04/06 13:58, 6年前 , 1F
你有 foo 不用還去創新的幹嘛?
04/06 13:58, 1F

04/06 15:12, 6年前 , 2F
他是強調同一時間,去get還未初始化的SingleInstance
04/06 15:12, 2F

04/06 15:15, 6年前 , 3F
magic static
04/06 15:15, 3F

04/06 15:51, 6年前 , 4F
看來你還是沒懂 foo->getInstance() 這個呼叫奇怪的
04/06 15:51, 4F

04/06 15:51, 6年前 , 5F
地方
04/06 15:51, 5F
不懂...

04/06 16:51, 6年前 , 6F
::
04/06 16:51, 6F
Orz 感謝 的確是應該 foo::getInstance() 但是如何避免同時兩個thread foo::getInstance() 呢?

04/06 17:04, 6年前 , 7F
mutex
04/06 17:04, 7F
7sarafciel: 起thread前先叫一次就好啦XD 04/06 17:14 這點我有提過,但面試RD說堅持要一起呼叫XD 剛剛看了一下 https://stackoverflow.com/questions/12248747/singleton-with-multithreads C++11中 直接宣告即可保證唯一 static Singleton* getSingletonInstance() { static Singleton instance; return &instance; } static Singleton& get() { static Singleton instance; return instance; } http://tinyurl.com/yxhagcvr in C++11 instance() does not need mutex in C++98 it does as two threads might enter it at once an start constructing object. C++11 ensures single initialization of static 'local' variables. C++98需要用mutex 而且此時mutex有被正確init嗎? 真的可以直接用? static foo *getInstance(){ if( instance == null ){ boost::lock_guard lock(m_mutex); if( instance == null ) instance = new foo(); } return instance; }

04/06 18:18, 6年前 , 8F
這只是把初始化的時機拉到跑執行緒以前而已,這跟你
04/06 18:18, 8F

04/06 18:18, 6年前 , 9F
另外呼叫一個函式初始化意義差不多
04/06 18:18, 9F
※ 編輯: kingofsdtw (1.169.137.208), 04/06/2019 20:09:28

04/06 22:51, 6年前 , 10F
mutex那個應該就是面試官想看的答案了
04/06 22:51, 10F

04/06 22:53, 6年前 , 11F
至於mutex 你能在static memeber function call到就代
04/06 22:53, 11F

04/06 22:54, 6年前 , 12F
表他也是static的 所以進entry point前就會建好了
04/06 22:54, 12F
謝謝大大回覆,等等我再多試幾次確認 另外... 當時我很直覺的用我的經驗回他 "雖然我知道mutex,但依照我的經驗getInstance沒人在用mutex的...." 然後他就變臉請我走了 ※ 編輯: kingofsdtw (1.169.137.208), 04/07/2019 15:00:24

04/07 19:19, 6年前 , 13F

04/07 19:19, 6年前 , 14F
g/thread-safe-initialization-of-a-singleton#comment-651
04/07 19:19, 14F
幫縮

04/07 19:34, 6年前 , 15F
C++11 Meyers Singleton Linux 只要0.04s ... -- 2019/04/07 21:04 總結一下 http://tinyurl.com/yxbrqxju http://tinyurl.com/y2z2wvo4 Before C++11 : Double-Checked Locking Pattern (DCLP) After C++11 : -Meyers Singleton -或手動關閉-fno-threadsafe-statics ※ 編輯: kingofsdtw (1.169.137.208), 04/07/2019 21:04:57

04/10 20:24, 6年前 , 16F
為了一個可以迂迴解決的問題 讓每次getInstance都要額
04/10 20:24, 16F

04/10 20:24, 6年前 , 17F
外開個鎖 老實講這種理論狂戰士要是跟他共事你要小心
04/10 20:24, 17F

04/10 20:25, 6年前 , 18F
不過Meyers Singleton的確很讚
04/10 20:25, 18F

04/12 19:14, 6年前 , 19F
為什麼有 std::call_once() 可以用卻把它晾在一邊?
04/12 19:14, 19F

05/01 00:58, 6年前 , 20F
其實你說一般 singleton 沒人在用 mutex 也是沒錯, 就當
05/01 00:58, 20F

05/01 00:58, 6年前 , 21F
躲過一個難共識的同事, 其實也是不虧
05/01 00:58, 21F
文章代碼(AID): #1Sg2wjnP (C_and_CPP)