我们知道std::deque::front()
会返回对deque的第一个元素的引用。
我想知道这段代码是否总是安全的:
//deque of lambdas
deque<function<void(void)>> funs;
// then is some other place:
// take a lock
m.lock();
auto f = move(funs.front()); // move the first lambda in f
funs.pop_front(); // remove the element from deque //now the value is hold by f
m_.unlock(); // unlock the resorce
f(); //execute f
我已经使用gcc-4.9尝试过这段代码并且有效,但我不知道我们是否可以认为这段代码是安全的!
答案 0 :(得分:8)
std::function
移动构造函数不保证不会抛出异常,因此您遇到异常安全问题。由于您没有为m
使用RAII锁定,因此如果auto f = move(funs.front());
抛出,它将保持锁定状态。您可以使用std::unique_lock
修正问题:
std::unique_lock<decltype(m)> lock{m};
if (!funs.empty()) {
auto f = move(funs.front()); // move the first lambda in f
funs.pop_front(); // remove the element from deque //now the value is hold by f
lock.unlock(); // unlock the resorce
f(); //execute f
}
或std::lock_guard
:
function<void()> f;
{
std::lock_guard<decltype(m)> lock{m};
if (!funs.empty()) {
f = move(funs.front()); // move the first lambda in f
funs.pop_front(); // remove the element from deque //now the value is hold by f
}
}
if (f) f(); //execute f