Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
Is it correct to destroy a mutex which is referenced but not owned by an unique_lock as in this code? { std::unique_ptr<std::mutex> mutex = std::make_unique<std::mutex>(); std::u...
Question
c++
#2: Post edited
- Is it correct to destroy a mutex which is referenced but not owned by an unique_lock as in this code?
{std::unique_ptr<std::mutex> mutex = std::make_unique<std::mutex>();std::unique_lock<std::mutex> lock(*mutex);lock.unlock();mutex.reset();} // Here lock is destroyed while holding an invalid reference to *mutex// but not having a lock on any mutex at all.- The example is somewhat convoluted and for this simple case it would be easier to not `lock.unlock();mutex.reset()` and let the destructors do their work. But there are other situations where being able to do this might come handy.
- It can be read in [std::unique_lock<Mutex>::~unique_lock](https://en.cppreference.com/w/cpp/thread/unique_lock/%7Eunique_lock) that :
- `Destroys the lock. If *this has an associated mutex and has acquired ownership of it, the mutex is unlocked.
- `
- Which leads me to believe that if the unique_lock no longer has ownership of the mutex the mutex is not unlocked so the no longer valid reference the mutex is not used in the destructor.
- Is it correct to destroy a mutex which is referenced but not owned by an unique_lock as in this code?
- ```c++
- {
- std::unique_ptr<std::mutex> mutex = std::make_unique<std::mutex>();
- std::unique_lock<std::mutex> lock(*mutex);
- lock.unlock();
- mutex.reset();
- } // Here lock is destroyed while holding an invalid reference to *mutex
- // but not having a lock on any mutex at all.
- ```
- The example is somewhat convoluted and for this simple case it would be easier to not `lock.unlock();mutex.reset()` and let the destructors do their work. But there are other situations where being able to do this might come handy.
- It can be read in [std::unique_lock<Mutex>::~unique_lock](https://en.cppreference.com/w/cpp/thread/unique_lock/%7Eunique_lock) that :
- `Destroys the lock. If *this has an associated mutex and has acquired ownership of it, the mutex is unlocked.
- `
- Which leads me to believe that if the unique_lock no longer has ownership of the mutex the mutex is not unlocked so the no longer valid reference the mutex is not used in the destructor.
#1: Initial revision
Destroy std::mutex referenced but not owned by std::unique_lock?
Is it correct to destroy a mutex which is referenced but not owned by an unique_lock as in this code? { std::unique_ptr<std::mutex> mutex = std::make_unique<std::mutex>(); std::unique_lock<std::mutex> lock(*mutex); lock.unlock(); mutex.reset(); } // Here lock is destroyed while holding an invalid reference to *mutex // but not having a lock on any mutex at all. The example is somewhat convoluted and for this simple case it would be easier to not `lock.unlock();mutex.reset()` and let the destructors do their work. But there are other situations where being able to do this might come handy. It can be read in [std::unique_lock<Mutex>::~unique_lock](https://en.cppreference.com/w/cpp/thread/unique_lock/%7Eunique_lock) that : `Destroys the lock. If *this has an associated mutex and has acquired ownership of it, the mutex is unlocked. ` Which leads me to believe that if the unique_lock no longer has ownership of the mutex the mutex is not unlocked so the no longer valid reference the mutex is not used in the destructor.