[問題] create singleInstance at same time
開發平台(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
04/06 13:58, 1F
→
04/06 15:12,
6年前
, 2F
04/06 15:12, 2F
推
04/06 15:15,
6年前
, 3F
04/06 15:15, 3F
→
04/06 15:51,
6年前
, 4F
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
04/06 17:04, 7F
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;
}
→
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
04/06 22:51, 10F
→
04/06 22:53,
6年前
, 11F
04/06 22:53, 11F
→
04/06 22:54,
6年前
, 12F
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, 13F
→
04/07 19:19,
6年前
, 14F
04/07 19:19, 14F
幫縮
→
04/07 19:34,
6年前
, 15F
04/07 19:34, 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
04/10 20:24, 16F
→
04/10 20:24,
6年前
, 17F
04/10 20:24, 17F
→
04/10 20:25,
6年前
, 18F
04/10 20:25, 18F
→
04/12 19:14,
6年前
, 19F
04/12 19:14, 19F
→
05/01 00:58,
6年前
, 20F
05/01 00:58, 20F
→
05/01 00:58,
6年前
, 21F
05/01 00:58, 21F