|
Boost : |
Subject: Re: [boost] Should pass boost::asio::io_service by raw pointer or smart pointer?
From: Vinnie Falco (vinnie.falco_at_[hidden])
Date: 2018-12-26 07:09:06
On Tue, Dec 25, 2018 at 11:03 PM Antony Polukhin <antoshkka_at_[hidden]> wrote:
> The version of the C++ Standard is relevant here. C++17 added guaranteed
> copy elisions and brought more order into evaluation order :) Does it allow to
> make a callback with stable address and an async waiting timer in it without
> a call to operator new?
C++ is not the issue here, it is the way that memory is allocated for
continuations. The implementation necessarily allocates memory to
store the I/O operation (since the initiating function returns to the
caller while the operation is outstanding). When the completion
handler is invoked, it is very likely that another asynchronous
operation will be performed. And thus, another memory allocation. This
predictable pattern of allocation permits an optimization: the
completion handler is moved to the stack before being invoked, and the
memory used to store the handler can be re-used for the next
asynchronous operation performed in the same context.
This is why completion handlers are MoveConstructible. The downside is
that none of the data members of the handler are stable (including the
`this` pointer). But it is more than made up for by the improved
performance.
You could, in theory, make your callback a shared_ptr or unique_ptr to
some stable data but you said you didn't want a unique_ptr.
Regards
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk