opendlang / opend

Boost Software License 1.0
84 stars 18 forks source link

alias load this #53

Closed rymrg closed 7 months ago

rymrg commented 7 months ago

This change allows more natural load of an atomic variable with sequential consistency.

IgorDeepakM commented 7 months ago

I was thinking about adding this, not with alias this but with the op*.

However, I didn't want to because it can induce errors in the code

Atomic:int x = 0;

// This is not atomic
if(x == 0 || x == 1)
{
...
}

Correct usage.

Atomic:int x = 0;

int k = x;
if(k == 0 || k == 1)
{
...
}

Also, how does alias load this coexist with opImplicitCast ?

rymrg commented 7 months ago

I was thinking about adding this, not with alias this but with the op*.

However, I didn't want to because it can induce errors in the code

Atomic:int x = 0;

// This is not atomic
if(x == 0 || x == 1)
{
...
}

Correct usage.

Atomic:int x = 0;

int k = x;
if(k == 0 || k == 1)
{
...
}

I'm not sure how is it different than the current state where the programmer might write:

Atomic:int x = 0;
if(x.load() == 0 || x.load() == 1)
{ ... }

In both cases the loads themselves are atomic, but the entire if isn't. It's the same if the programmer writes the following:

Atomic:int x = 0;
int t = x.load();
x = 5;

instead of

Atomic:int x = 0;
int t = x.exhcnage(5);

Also, how does alias load this coexist with opImplicitCast ?

If I understand correctly, opImplicitCast will be called first if exists but otherwise the alias takes place.

IgorDeepakM commented 7 months ago

This change still complies with https://en.cppreference.com/w/cpp/atomic/atomic. I assume C++ has a more generous implicit cast which makes it possible to use in expressions as well.