在C ++ 11中从std :: deque移动一个元素

时间:2014-06-15 20:32:41

标签: c++ c++11 std move deque

我们知道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尝试过这段代码并且有效,但我不知道我们是否可以认为这段代码是安全的!

1 个答案:

答案 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