Max*_*rai 0 c++ multithreading scope
我们和朋友谈论功能和多线程.示例代码是:
void SomeClass::Foo()
{
std::lock_guard<std::mutex> lock(mMutexObj);
statement1;
statement2;
statement3;
}
Run Code Online (Sandbox Code Playgroud)
因此,我们知道,有时编译器会在需要的地方内联函数.在这种情况下是否有可能:编译器内联Foo函数,所有3个语句都运行并且lock_guard不起作用,因为范围不在此处结束且没有析构函数调用:
// Inlined operations
std::lock_guard<std::mutex> lock(mMutexObj);
statement1;
statement2;
statement3;
// Global scope, where function was inlined continues here...
global statement1;
global statement2;
...;
Run Code Online (Sandbox Code Playgroud)
可能吗?编译器将内联这样的函数的百分之几,或者我不理解内联函数的范围?
如果函数内联或不内联,或者内联或不内联声明,程序的可观察行为将不会更改.在任何一种情况下,都会在适当的地方调用lock_guard的析构函数.同样,函数中的静态变量指的是相同的静态变量,即使它是内联的(d)也是如此.