Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

83%
+8 −0
Q&A Destroy std::mutex referenced but not owned by std::unique_lock?

No, such an operation is not safe. The documentation of std::unique_lock in the standard states that it's UB for the mutex do be destroyed while the lock still has a pointer to it. However, there ...

posted 3y ago by Angew‭  ·  edited 3y ago by Angew‭

Answer
#2: Post edited by user avatar Angew‭ · 2021-02-05T10:26:22Z (about 3 years ago)
Add standard quotes
  • No, such an operation is not safe. The documentation of `std::unique_lock` in the standard states that it's UB for the mutex do be destroyed while the lock still has a pointer to it.
  • However, there is a way to dissociate the mutex from the lock: calling `release` on the lock. That resets the lock's internal mutex pointer to null. So the code would be valid like this:
  • ```
  • {
  • std::unique_ptr<std::mutex> mutex = std::make_unique<std::mutex>();
  • std::unique_lock<std::mutex> lock(*mutex);
  • lock.unlock();
  • lock.release();
  • mutex.reset();
  • }
  • ```
  • No, such an operation is not safe. The documentation of `std::unique_lock` in the standard states that it's UB for the mutex do be destroyed while the lock still has a pointer to it.
  • However, there is a way to dissociate the mutex from the lock: calling `release` on the lock. That resets the lock's internal mutex pointer to null. So the code would be valid like this:
  • ```
  • {
  • std::unique_ptr<std::mutex> mutex = std::make_unique<std::mutex>();
  • std::unique_lock<std::mutex> lock(*mutex);
  • lock.unlock();
  • lock.release();
  • mutex.reset();
  • }
  • ```
  • ---
  • Here are the relevant standard quotes from C++2a (N4680) 32.5.4.3. [thread.lock.unique] (`pm` is an exposition-only pointer to the mutex associated with the lock):
  • > 32.5.4.3/1 The behavior of a program is undefined if the contained pointer `pm` is not null and the lockable object pointed
  • to by `pm` does not exist for the entire remaining lifetime (6.7.3) of the `unique_lock` object.
  • > 32.5.4.3.3
  • >
  • > ```
  • > mutex_type* release() noexcept;
  • > ```
  • >
  • > 2 *Returns:* The previous value of `pm`.
  • >
  • > 3 *Postconditions:* `pm == 0` and `owns == false`.[]()[]()
#1: Initial revision by user avatar Angew‭ · 2021-02-05T10:21:19Z (about 3 years ago)
No, such an operation is not safe. The documentation of `std::unique_lock` in the standard states that it's UB for the mutex do be destroyed while the lock still has a pointer to it.

However, there is a way to dissociate the mutex from the lock: calling `release` on the lock. That resets the lock's internal mutex pointer to null. So the code would be valid like this:

```
{
  std::unique_ptr<std::mutex> mutex = std::make_unique<std::mutex>();
  std::unique_lock<std::mutex> lock(*mutex);
  lock.unlock();
  lock.release();
  mutex.reset();
}
```