C++ Logo

std-proposals

Advanced search

Re: [std-proposals] volatile is not well supported

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Fri, 2 Sep 2022 23:36:25 +0200
pt., 2 wrz 2022 o 18:15 Phil Bouchard via Std-Proposals
<std-proposals_at_[hidden]> napisaƂ(a):
>
>
> On 9/1/22 19:37, Charles Milette via Std-Proposals wrote:
>
> > I believe you want an atomic<shared_ptr<int>>, not volatile.
>
> Ok actually I don't need the variable to be atomic, I just need it to be reloaded. Also I wanted the entire struct to be volatile, not just a few fundamentals members.
>
>

You need to use atomic, "reloaded" value do not give you any
guarantees to write correct portable multithreaded application.
By standard volatale `*cData.p` can easily return you the correct
pointer but defer wrong value because it had cached value from some
other operation
in this thread. atomic pointer guarantees that if you get a new
pointer value then the defred value of that pointer will be correct
too.

>
>
> On Thu, Sep 1, 2022 at 7:24 PM Phil Bouchard via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>
>> Greetings,
>>
>> In this age of multicores and multithreaded applications, today at work I did try to mark an entire struct as volatile and I couldn't compile a simple application. Here is a simple replicate:
>>
>>
>> #include <memory>
>>
>> using namespace std;
>>
>> struct Data
>> {
>> shared_ptr<int> p;
>> };
>>
>> int main()
>> {
>> static Data volatile cData;
>>
>> cData.p = make_shared<int>(9);
>>
>> return 0;
>> }
>>
>> volatile.cpp:14:13: error: no viable overloaded '='
>> cData.p = make_shared<int>(9);
>>
>>
>> I think it would be convenient to fix this issue in this age of parallel programming.
>>
>>
>> Regards,
>>

Received on 2022-09-02 21:36:37