Date: Tue, 13 Dec 2022 15:20:45 +0100
Il 13/12/22 13:47, Frederick Virchanza Gotham via Std-Proposals ha scritto:
> So here's what I did:
>
> std::optional<Dialog_Waiting> dlg;
>
> this->worker_thread = std::jthread(
> Thread_Entry_Point,
> static_cast<Dialog_Waiting*>(static_cast<void*>(&dlg)) ); //
> yeah yeah I know I know
>
> dlg.emplace( this, this->worker_thread.get_stop_source() );
But this particular example, how do you avoid a race on `dlg` (and
`*dlg`? If there's a synchronization protocol in place, can't that
protocol be used to pass the dialog's address to the thread?
`optional` sounds a bit sketchy as a vocabulary type here, as it's
effectively always loaded; you're just using it for the convenience. I
can't come up with a better idea that isn't a hand-rolled optional
giving you access to the storage, like you propose...
> Would it not be better to give std::optional an extra member function
> to get the address of 'where the object would be constructed',
> something like:
>
> template<typename T>
> class optional {
> alignas(T) char unsigned buf[sizeof(T)];
>
> public:
> T * GetAddress(void) const
> {
> return static_cast<T*>(static_cast<void*>(buf + 0u));
> }
> };
>
> If we were to add this feature to C++26, then we could simply give the
> following caveat in:
> "Behaviour is undefined if you dereference the return value
> from std::optional<T>::GetAddress when *this does not contain
> a value."
But then you also need then a way to tell optional "you're now loaded
with a value that I have externally built into your storage"?
Thanks,
> So here's what I did:
>
> std::optional<Dialog_Waiting> dlg;
>
> this->worker_thread = std::jthread(
> Thread_Entry_Point,
> static_cast<Dialog_Waiting*>(static_cast<void*>(&dlg)) ); //
> yeah yeah I know I know
>
> dlg.emplace( this, this->worker_thread.get_stop_source() );
But this particular example, how do you avoid a race on `dlg` (and
`*dlg`? If there's a synchronization protocol in place, can't that
protocol be used to pass the dialog's address to the thread?
`optional` sounds a bit sketchy as a vocabulary type here, as it's
effectively always loaded; you're just using it for the convenience. I
can't come up with a better idea that isn't a hand-rolled optional
giving you access to the storage, like you propose...
> Would it not be better to give std::optional an extra member function
> to get the address of 'where the object would be constructed',
> something like:
>
> template<typename T>
> class optional {
> alignas(T) char unsigned buf[sizeof(T)];
>
> public:
> T * GetAddress(void) const
> {
> return static_cast<T*>(static_cast<void*>(buf + 0u));
> }
> };
>
> If we were to add this feature to C++26, then we could simply give the
> following caveat in:
> "Behaviour is undefined if you dereference the return value
> from std::optional<T>::GetAddress when *this does not contain
> a value."
But then you also need then a way to tell optional "you're now loaded
with a value that I have externally built into your storage"?
Thanks,
-- Giuseppe D'Angelo
Received on 2022-12-13 14:20:49