www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Improve the ergonomics of core.atomic

reply IGotD- <nise nise.com> writes:
Currently the core.atomic library just adds template functions 
for atomic operations and not a special atomic type. This forces 
the programmer to use long a rather ugly template operations 
through out the entire code.

Typically

atomicOp!"+="(v1, v2);

This is nice for casual atomic operations but ugly and a pain for 
many atomic operations.

Why hasn't D just like C++ an atomic type. In C++ you simply 
declare.

std::atomic<int> var;

and then use the operator overload.

var += 1;

which will trickle down to var.fetch_add(1);

This makes much nicer in the code. Also if you want swap between 
non atomic operations and atomic operations, you have to change 
less.

What D needs is just like in C++ an atomic template type.
Oct 01 2020
next sibling parent reply Jackel <jackel gmx.com> writes:
On Thursday, 1 October 2020 at 13:27:46 UTC, IGotD- wrote:
 Currently the core.atomic library just adds template functions 
 for atomic operations and not a special atomic type. This 
 forces the programmer to use long a rather ugly template 
 operations through out the entire code.

 Typically

 atomicOp!"+="(v1, v2);

 This is nice for casual atomic operations but ugly and a pain 
 for many atomic operations.

 Why hasn't D just like C++ an atomic type. In C++ you simply 
 declare.

 std::atomic<int> var;

 and then use the operator overload.

 var += 1;

 which will trickle down to var.fetch_add(1);

 This makes much nicer in the code. Also if you want swap 
 between non atomic operations and atomic operations, you have 
 to change less.

 What D needs is just like in C++ an atomic template type.
One reason could be that it is much more obvious what operations are atomic when reading. The syntax isn't as nice, but you know the operation is atomic.
Oct 01 2020
parent IGotD- <nise nise.com> writes:
On Thursday, 1 October 2020 at 14:35:01 UTC, Jackel wrote:
 One reason could be that it is much more obvious what 
 operations are atomic when reading. The syntax isn't as nice, 
 but you know the operation is atomic.
The current atomic template functions don't need to be removed and they can be used if the programmer wish to use them just as before. A new atomic template type would just be an addition that doesn't break old code.
Oct 01 2020
prev sibling parent Denis Feklushkin <feklushkin.denis gmail.com> writes:
On Thursday, 1 October 2020 at 13:27:46 UTC, IGotD- wrote:

(Probably)

 atomicOp!"+="(v1, v2);
This is atomic CPU instruction call.
 This is nice for casual atomic operations but ugly and a pain 
 for many atomic operations.

 Why hasn't D just like C++ an atomic type. In C++ you simply 
 declare.

 std::atomic<int> var;
(Probably) this template isn't provides ability to use var by non-atomic instructions.
Oct 01 2020