Atomic updates

bearophile bearophileHUGS at lycos.com
Tue Jan 22 07:44:55 PST 2013


> In core.atomic I think there is what's needed, cas and atomicOp:

I know what a cas is, but I don't have experience on this kind of 
coding. First try at a translation (doesn't work yet, and the 
global lock on buckets is missing still):


import core.atomic: atomicLoad, atomicOp, cas;
...

     public void transfer(in size_t from, in size_t to,
                          TBucketValue amount) {
         if (from == to) // Needed?
             return;
         buckets.RLock();
         while (true) {
             auto v1 = atomicLoad(buckets[from].value);
             if (amount > v1)
                 amount = v1;
             if (cas(&buckets[from].value, v1, v1 - amount)) {
                 atomicOp!"+="(buckets[to].value, amount);
                 break;
             }
             // Else retry.
         }
         buckets.RUnlock();
         transfersCount++;
     }


Bye,
bearophile


More information about the Digitalmars-d-learn mailing list