jeudi 6 mai 2021

Do I need to call unlock() at the end of the loop (for a local unique_lock)?

Is this:

std::mutex mutex;
for () {
    std::unique_lock<mutex> lock(mutex);
    // do something
    lock.unlock();
}

Equivalent to this in terms of releasing and acquiring the lock every iteration of the for loop?

std::mutex mutex;
for () {
    std::unique_lock<mutex> lock(mutex);
    // do something
}

Related questions:

  1. Is using unique_lock in new scope equivalent to unlock call at the end of work with shared resource?
  2. boost scoped_lock on mutex in while(1) loop
  3. Creating new pointer object in loop
  4. Reusing a unique_lock in a consumer loop

Aucun commentaire:

Enregistrer un commentaire