>Ian Appru wrote:
>> While atomic increment/decrements (using cas) are suffiently well documented 
>> for me to be pretty
 confident in my implementation I would apreciate any input for an atomic 
conditional increment.
>
>I'd expect the following to work on OpenSolaris:
>
>#include <atomic.h>
>
>int atomic_conditional_increment(int *target)
>{
>     for (;;) {
>         int old = *target;
>         if (old == 0)
>             return 0;
>
>         if (atomic_cas_uint((uint_t *) target, old, old + 1) == old)
>             return old + 1;
>     }
>}

You will certainly need a membar before reading *target.

(If not, it can happen that one thread continuously sees "0" while
another CPU runs away incrementing the counter.

(And you function has a bug in that it will not always update a non-zero
target)

Casper
_______________________________________________
opensolaris-discuss mailing list
[email protected]

Reply via email to