Closed
Description
[thread.lock.guard] p1 reads
A lock_guard object maintains ownership of a lockable object throughout the lock_guard object's lifetime.
Yet in the following (ostensibly) valid program ownership is not maintained throughout the lock_guard object's lifetime:
std::lock_guard<std::mutex> lock(mutex);
new (&lock) char[sizeof lock]; // http://eel.is/c++draft/basic.life#1.5
std::mutex dummy_mutex;
new (&lock) std::lock_guard<std::mutex>(dummy_mutex);
Activity
CaseyCarter commentedon Oct 5, 2023
I don't understand. Are you suggesting that
lock
somehow relinquishes ownership ofmutex
beforenew (&lock) char[sizeof lock]
ends its lifetime by reusing its storage to hold an array of characters?There are two objects of type
std::lock_guard<std::mutex>
in this program. One that ownsmutex
during its entire lifetime, and one that ownsdummy_mutex
during its entire lifetime. Both objects occupy the same storage but at different times.jeremy-rifkin commentedon Oct 5, 2023
Thank you for your quick response, I'm happy to clarify
No, that'd be (more or less) impossible.
Let me preface this by saying it's entirely possible I'm misreading / misunderstanding but to me [thread.lock.guard] p1 implies that it should release the mutex when its lifetime ends. But in this case the
mutex
is not unlocked.CaseyCarter commentedon Oct 9, 2023
"I check my email frequently while I am at work" implies nothing about my email-checking frequency when I'm at home. I might not check at all, or check rarely, or check frequently. None of those would contradict the fact that I check frequently while I am at work.
Similarly, "A lock_guard object maintains ownership of a lockable object throughout the lock_guard object's lifetime" says nothing about what a
lock_guard
object may or may not do outside its lifetime. A zombielock_guard
continuing to own a lockable object may not be ideal, but it still doesn't contradict a statement about the behavior of livinglock_guard
objects.