c++ std::lock_guard 作用域到达
c++ std::lock_guard scope reach
假设我有一个获取锁并执行由参数传递的函数的函数:
template <typename T>
T acquireLock(std::mutex& _lock, const std::function<T()>& execution) {
try {
std::lock_guard<std::mutex> mutex (_lock);
return execution();
} catch (std::logic_error& error) {
std::cerr << "[exception caught]\n\t" << error.what() << std::endl;
}
return false;
}
此外,我有一个 class 需要为其某些方法获取所述锁。
class MyThreadSafeClass {
public:
bool Init();
bool StopApi();
unsigned int GetValue() {
auto ret = acquireLock<unsigned int>(_lock, [this]() -> unsigned int {
// does some work that's not thread-safe...
return value;
});
return ret;
}
private:
bool _ready = false;
std::mutex _lock;
};
我怀疑每当我调用 GetValue()
时,看看我的 acquireLock()
方法,execution()
调用是否也受锁定范围的影响?
auto myClass = new MyThreadSafeClass();
myClass->GetValue();
查看 this,更具体地说:
When a lock_guard object is created, it attempts to take ownership of
the mutex it is given. When control leaves the scope in which the
lock_guard object was created, the lock_guard is destructed and the
mutex is released.
我仍然不清楚 execution()
代码内部发生的事情是否仍然受锁定范围的影响。
- The copy-initialization of the result of the call is sequenced before the destruction of temporaries at the end of the full-expression established by the operand of the return statement, which, in turn, is sequenced before the destruction of local variables ([stmt.jump]) of the block enclosing the return statement.
所以我们得到:
- 互斥量已锁定
execution()
在持有锁时被调用
- 锁被释放
- 计算值返回给调用者(或输入
catch
子句)
换句话说,是的,它将按预期工作。
无关说明:std::function
效率不高。对可调用类型进行模板化应该会更好:
template<typename F>
auto doLocked(std::mutex& _lock, F const& f) -> decltype(f()) {
std::lock_guard<std::mutex> lock(_lock);
return f();
}
假设我有一个获取锁并执行由参数传递的函数的函数:
template <typename T>
T acquireLock(std::mutex& _lock, const std::function<T()>& execution) {
try {
std::lock_guard<std::mutex> mutex (_lock);
return execution();
} catch (std::logic_error& error) {
std::cerr << "[exception caught]\n\t" << error.what() << std::endl;
}
return false;
}
此外,我有一个 class 需要为其某些方法获取所述锁。
class MyThreadSafeClass {
public:
bool Init();
bool StopApi();
unsigned int GetValue() {
auto ret = acquireLock<unsigned int>(_lock, [this]() -> unsigned int {
// does some work that's not thread-safe...
return value;
});
return ret;
}
private:
bool _ready = false;
std::mutex _lock;
};
我怀疑每当我调用 GetValue()
时,看看我的 acquireLock()
方法,execution()
调用是否也受锁定范围的影响?
auto myClass = new MyThreadSafeClass();
myClass->GetValue();
查看 this,更具体地说:
When a lock_guard object is created, it attempts to take ownership of the mutex it is given. When control leaves the scope in which the lock_guard object was created, the lock_guard is destructed and the mutex is released.
我仍然不清楚 execution()
代码内部发生的事情是否仍然受锁定范围的影响。
- The copy-initialization of the result of the call is sequenced before the destruction of temporaries at the end of the full-expression established by the operand of the return statement, which, in turn, is sequenced before the destruction of local variables ([stmt.jump]) of the block enclosing the return statement.
所以我们得到:
- 互斥量已锁定
execution()
在持有锁时被调用- 锁被释放
- 计算值返回给调用者(或输入
catch
子句)
换句话说,是的,它将按预期工作。
无关说明:std::function
效率不高。对可调用类型进行模板化应该会更好:
template<typename F>
auto doLocked(std::mutex& _lock, F const& f) -> decltype(f()) {
std::lock_guard<std::mutex> lock(_lock);
return f();
}