【发布时间】:2019-02-01 15:56:05
【问题描述】:
我发现下面的代码 CCriticalSection 不能正常工作。
foo()
{
//...
{
static CCriticalSection cs; //static local variable
CSingleLock lock(&cs, TRUE);
//Critical Section
non_reentrant_function();
}
//...
}
但是这些代码很好:
static CCriticalSection cs; //define a global static variable
foo()
{
//...
{
CSingleLock lock(&cs, TRUE);
//Critical Section
non_reentrant_function();
}
//...
}
众所周知,静态局部变量只有在第一次调用函数 foo 时才会被初始化。
如果多线程调用 foo,静态局部 CCriticalSection 会发生什么情况?
【问题讨论】:
-
“不起作用”——见How to Ask。
标签: multithreading mfc