作者kingofsdtw (不能閒下來!!)
看板C_and_CPP
標題[問題] create singleInstance at sametime
時間Sat Apr 6 12:44:25 2019
開發平台(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://webptt.com/m.aspx?n=bbs/C_and_CPP/M.1554525869.A.C59.html
1F:→ loveme00835: 你有 foo 不用還去創新的幹嘛? 04/06 13:58
2F:→ kingofsdtw: 他是強調同一時間,去get還未初始化的SingleInstance 04/06 15:12
3F:推 lovejomi: magic static 04/06 15:15
4F:→ loveme00835: 看來你還是沒懂 foo->getInstance() 這個呼叫奇怪的 04/06 15:51
5F:→ loveme00835: 地方 04/06 15:51
不懂...
6F:→ aiwhat: :: 04/06 16:51
Orz 感謝 的確是應該 foo::getInstance()
但是如何避免同時兩個thread foo::getInstance() 呢?
7F:推 idiont: mutex 04/06 17:04
7
→ sarafciel: 起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;
}
8F:→ loveme00835: 這只是把初始化的時機拉到跑執行緒以前而已,這跟你 04/06 18:18
9F:→ loveme00835: 另外呼叫一個函式初始化意義差不多 04/06 18:18
※ 編輯: kingofsdtw (1.169.137.208), 04/06/2019 20:09:28
10F:→ sarafciel: mutex那個應該就是面試官想看的答案了 04/06 22:51
11F:→ sarafciel: 至於mutex 你能在static memeber function call到就代 04/06 22:53
12F:→ sarafciel: 表他也是static的 所以進entry point前就會建好了 04/06 22:54
謝謝大大回覆,等等我再多試幾次確認
另外...
當時我很直覺的用我的經驗回他
"雖然我知道mutex,但依照我的經驗getInstance沒人在用mutex的...."
然後他就變臉請我走了
※ 編輯: kingofsdtw (1.169.137.208), 04/07/2019 15:00:24
14F:→ layan: g/thread-safe-initialization-of-a-singleton#comment-651 04/07 19:19
幫縮
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
16F:→ Killercat: 為了一個可以迂迴解決的問題 讓每次getInstance都要額 04/10 20:24
17F:→ Killercat: 外開個鎖 老實講這種理論狂戰士要是跟他共事你要小心 04/10 20:24
18F:→ Killercat: 不過Meyers Singleton的確很讚 04/10 20:25
19F:→ tinlans: 為什麼有 std::call_once() 可以用卻把它晾在一邊? 04/12 19:14
20F:→ uranusjr: 其實你說一般 singleton 沒人在用 mutex 也是沒錯, 就當 05/01 00:58
21F:→ uranusjr: 躲過一個難共識的同事, 其實也是不虧 05/01 00:58