Skip to content

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

Closed
@jeremy-rifkin

Description

@jeremy-rifkin

[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

CaseyCarter commented on Oct 5, 2023

@CaseyCarter
Contributor

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

jeremy-rifkin commented on Oct 5, 2023

@jeremy-rifkin
Author

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

CaseyCarter commented on Oct 9, 2023

@CaseyCarter
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @CaseyCarter@jensmaurer@jeremy-rifkin

        Issue actions

          [thread.lock.guard] Does not take into account storage reuse · Issue #6588 · cplusplus/draft