Skip to content

[basic.stc.dynamic.allocation] p2 "was subsequently passed to a replaceable deallocation function" is vague in a multithread context #895

@xmh0511

Description

@xmh0511

Full name of submitter (unless configured in github; will be published with the issue): Jim X

Consider this example:

#include <thread>
#include <atomic>

int main(){
   std::atomic<bool> flag = false;
   std::jthread t1([&](){
      auto p = operator new(sizeof(int));  // #1
      while(!flag.load(std::memory_order::acquire));
      operator delete(p); // #2
   });

  std::jthread t2([&](){
    auto p2 = operator new(sizeof(int));  // #3
    flag.store(true,std::memory_order::release);
  });
}

In this example, both #1 and #3 happen-before #2. Can both #1 and #3 return the same address? The relevant rule is [basic.stc.dynamic.allocation] p2

If the request succeeds, the value returned by a replaceable allocation function is a non-null pointer value ([basic.compound]) p0 different from any previously returned value p1, unless that value p1 was subsequently passed to a replaceable deallocation function.

In a multithread context, it's hard to say what "previously returned" and "was subsequently passed to a replaceable deallocation function" mean. There is no global timeline across different threads.

Suggested Resolution:

We may need to define what "previously returned" and "was subsequently passed to a replaceable deallocation function" in terms of happens-before.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions