how to stop a variable from being optimized out

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Feb 24 10:30:07 PST 2015


On Tue, 24 Feb 2015 12:50:53 +0000, Rory wrote:

> On Tuesday, 24 February 2015 at 06:48:26 UTC, ketmar wrote:
>> but why do you need this? just use `atomicLoad` to get shared variable
>> value, it will do the right caching.
> 
> Nice! Thanks. Tested atomicLoad and it is slightly faster for my non
> blocking queue.

just in case: by "do the right caching" i meant that this is memory 
barrier, so compiler will not optimise your `current` away. i.e. the only 
thing you have to change is this line:

  volatile auto current = payload; // payload is a shared variable

to:

  auto current = atomicLoad(payload);

i reread my answer and found that it may be not so obvious.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150224/4967c039/attachment-0001.sig>


More information about the Digitalmars-d-learn mailing list