From what I have understood, std::unique_lock is a kind of wrapper around the underlying mutex object so as to provide a safer implementation over using raw mutexes (e.g., end up in unlocked state if an exception is thrown or on destruction). Is this all std::unique_lock is for?
Try #1
std::mutex m;  // global 
void foo() {
  m.lock();
  // critical section
  m.unlock();
}
Try #2
std::mutex m;  // global 
void foo() {
  std::unique_lock<std::mutex> ul(m);
  // critical section
}
Is Try #2 preferred over Try #1, and is this what std::unique_lock is for? Please provide some other examples where std::unique_lock may be desired.
Aucun commentaire:
Enregistrer un commentaire