作者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/cn.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