digitalmars.D - ++x vs. atomicOp!"+="(x,1) with a shared int
- Chuck Allison (24/24) Jun 01 2014 I was under the impression that calling ++x for a shared x is an
- Meta (3/27) Jun 01 2014 It is a known bug with an open pull request to fix it.
- Chuck Allison (2/4) Jun 01 2014 Thanks!
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (3/4) Jun 01 2014 Is this design for the sake of explicitness so the D developer
I was under the impression that calling ++x for a shared x is an error. Not only do I not get an error, the effect of ++x is identical to atomicOp"+="(x,1) in the following example (the variable is count here, not x): shared int count; void f(string s) { foreach (i; 0..100) writefln("%s: %s", ++count, s); } void main() { spawn(&f,"Dessert Topping"); spawn(&f,"Floor Wax"); } I get the same results if I change f like so: void f(string s) { foreach (i; 0..100) { atomicOp!"+="(count,1); writefln("%s: %s", count, s); } } Is ++ now atomic for shared ints? I'm just wondering why the first version of f works at all, when TDPL says it should be an error. Thanks.
Jun 01 2014
On Sunday, 1 June 2014 at 07:06:27 UTC, Chuck Allison wrote:I was under the impression that calling ++x for a shared x is an error. Not only do I not get an error, the effect of ++x is identical to atomicOp"+="(x,1) in the following example (the variable is count here, not x): shared int count; void f(string s) { foreach (i; 0..100) writefln("%s: %s", ++count, s); } void main() { spawn(&f,"Dessert Topping"); spawn(&f,"Floor Wax"); } I get the same results if I change f like so: void f(string s) { foreach (i; 0..100) { atomicOp!"+="(count,1); writefln("%s: %s", count, s); } } Is ++ now atomic for shared ints? I'm just wondering why the first version of f works at all, when TDPL says it should be an error. Thanks.It is a known bug with an open pull request to fix it. https://github.com/D-Programming-Language/dmd/pull/3070
Jun 01 2014
On Sunday, 1 June 2014 at 07:23:25 UTC, Meta wrote:It is a known bug with an open pull request to fix it. https://github.com/D-Programming-Language/dmd/pull/3070Thanks!
Jun 01 2014
Is this design for the sake of explicitness so the D developer doesn't unknowingly add dependencies on atomics? /Perhttps://github.com/D-Programming-Language/dmd/pull/3070
Jun 01 2014