Skip to content

std::atomic<std::shared_ptr>::wait does not seem to care about control block difference. Is this a bug? #3602

@zwhfly

Description

@zwhfly

Discussed in #3555

Originally posted by zwhfly March 10, 2023
This code seems to block forever under MSVC, but returns successfully under gcc/clanglibstdc++.

#include <memory>
#include <atomic>


int main()
{
    auto sp1 = std::make_shared<char>();
    auto holder = [sp1] {};
    auto sp2 = std::make_shared<decltype(holder)>(holder);
    std::shared_ptr<char> sp3{ sp2, sp1.get() };

    std::atomic<std::shared_ptr<char>> asp{ sp1 };
    asp.wait(sp3);

    return 0;
}

It is said here that

(atomic<shared_ptr>::wait) compares load(order) with old and if they are equivalent then blocks ...

Notes: two shared_ptrs are equivalent if they store the same pointer and either share ownership or are both empty.

I thought that sp1 and sp3 do not have the same control block, so they do not share ownership. Right? So the wait call should return immediately.

But by checking the source code, it seems that the STL implementation for std::atomic<std::shared_ptr>::wait compares the object pointer (the char *) only. It does not compare the control block pointer at all.

Is this a bug in STL? Or what am I missing here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions