Possible bug in atomicOp

dsimcha dsimcha at yahoo.com
Sat Oct 23 05:52:09 PDT 2010


== Quote from Benjamin Thaut (code at benjamin-thaut.de)'s article
> The following testcase (when executed on dual core at least) results in
> a endless loop inside atomicOp.
> import std.stdio;
> import std.concurrency;
> enum Messages {
> 	GO,
> 	END
> }
> shared class Account {
> 	private double amount = 0;
> 	double getAmount() const {
> 		return amount;
> 	}
> 	void change(double change){
> 		atomicOp!"+="(amount,change);
> 	}
> }
> shared Account bank = null;
> void otherThread(Tid father){
> 	send(father,Messages.GO);
> 	for(int i=0;i<1000;i++)
> 		bank.change(-100);
> 	send(father,Messages.END);
> }
> void main(string[] args)
> {
> 	bank = new Account();
> 	spawn(&otherThread,thisTid);
> 	receiveOnly!(Messages)();
> 	for(int i=0;i<1000;i++)
> 		bank.change(+100);
> 	receiveOnly!(Messages)();
> 	writefln("Program finished. Amount is %s",bank.getAmount());
> }
> Is this a bug, or am I doing something wrong here?
> If it is a bug it is kind of critical because people which are reading
> the "the D programming language" book won't be happy to find out that
> some of the given examples do not work yet.

http://d.puremagic.com/issues/show_bug.cgi?id=4782

Basically, atomicLoad (which atomicOp uses) always returns in ALU registers.
Floating point numbers need to be returned in floating point registers.
Therefore, a NaN always gets returned from atomicLoad!double, and a NaN isn't
equal to anything.


More information about the Digitalmars-d mailing list