Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[thread.lock.guard] Does not take into account storage reuse #6588

Closed
jeremy-rifkin opened this issue Oct 5, 2023 · 3 comments
Closed

[thread.lock.guard] Does not take into account storage reuse #6588

jeremy-rifkin opened this issue Oct 5, 2023 · 3 comments

Comments

@jeremy-rifkin
Copy link

jeremy-rifkin commented Oct 5, 2023

[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);
@CaseyCarter
Copy link
Contributor

CaseyCarter commented Oct 5, 2023

I don't understand. Are you suggesting that lock somehow relinquishes ownership of mutex before new (&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 owns mutex during its entire lifetime, and one that owns dummy_mutex during its entire lifetime. Both objects occupy the same storage but at different times.

@jeremy-rifkin
Copy link
Author

jeremy-rifkin commented Oct 5, 2023

Thank you for your quick response, I'm happy to clarify

Are you suggesting that lock somehow relinquishes ownership of mutex before new (&lock) char[sizeof lock] ends its lifetime by reusing its storage to hold an array of characters?

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
Copy link
Contributor

"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 zombie lock_guard continuing to own a lockable object may not be ideal, but it still doesn't contradict a statement about the behavior of living lock_guard objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants