C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Copy-construct, move-construct, and PR-construct

From: Thiago Macieira <thiago_at_[hidden]>
Date: Mon, 21 Aug 2023 15:24:24 -0700
On Monday, 21 August 2023 14:59:36 PDT Frederick Virchanza Gotham via Std-
Proposals wrote:
> extern mutex FuncThatReturnsMutex(int,int,int,int,int);

Can you explain why a function would return a mutex in the first place?

Mutexes aren't copyable or movable *because* their address matters. A mutex at
a different address is not the same mutex, even if it has been locked and
unlocked exactly as many times.

Semantically, this function shouldn't *return* a mutex. It should receive a
mutex by reference so it can lock and unlock it as necessary. And if the
purpose of the function is to create said mutex, then you've already stumbled
upon the correct container to pass it: optional<mutex>.

void FunctionThatCreatesMutex(std::optional<mutex> &where);

If you want to avoid the 100% overhead incurred by std::optional, you can use
std::unique_ptr and accept the penalty of the indirection. Or you can avoid
both if you can accept the non-RAII-safe implementation:

void FunctionThatCreatesMutex(std::mutex *where); // or void*


If you want to argue that the above is cumbersome or prone to failure or hard
to maintain or any of a series of possible drawbacks, you can do that. But you
must start by examining the state of the art and explaining those things,
which you haven't done. Moreover, you must illustrate how widespread the
problem is and/or how many other similar use-cases could benefit from a similar
solution.

You don't start with the solution in search of a problem.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-08-21 22:24:27