std::mutex
有范围吗?我知道在JAVA中,我们可以使用{}
来限制锁的范围,以尽快释放锁。如果我们可以在c ++中做同样的事情?
void func() {
// some code
{
std::lock_guard<std::mutex> lock(mutex_);
}
// some code
}
另外,互斥锁什么时候起作用?锁定在进入函数后会立即起作用,还是会一直执行到当前行被锁定为止?编译器对代码优化有影响吗?
void func() {
// some code
std::lock_guard<std::mutex> lock(mutex_); // mutex work here or as soon as it enters the function
// some code
}