[concurrency-interest] AutomicBoolean toggleAndGet()

Martin Buchholz martinrb at google.com
Fri Feb 12 13:15:30 EST 2010


On Fri, Feb 12, 2010 at 08:56, David M. Lloyd <david.lloyd at redhat.com> wrote:
> There are several "missing" atomic operations and classes that I've wished
> existed at one point or another - though many of these can be implemented in
> terms of others of these:
>
> // replace int with long for AtomicLong* variants, add "int index" for
> *Array variants
> - int getAndSetBits(int original, int bits);    // value |= bits
> - int getAndClearBits(int original, int bits);  // value &= bits or value &=
> ~bits, either way
> - int getAndToggleBits(int original, int bits); // value ^= bits

These all seem like plausible additions.
But there's no way to "complete" the API this way,
since that would mean supporting all possible operations on ints.

In a language with first class functions, we could have

int applyAndGet(Function<int,int> operation)

In any case, Doug is the person you will really need to convince.

Martin

> One could also add atomic shift & rotate as well, though I don't know of a
> use case offhand for that.
>
> And it would be nice if there were an AtomicReferenceArrayFieldUpdater class
> as well; that always seemed like an odd omission.
>
> - DML
>
> On 02/12/2010 10:34 AM, Guy Korland wrote:
>>
>> Leaving aside the fact that it completes the AtomicX API.
>> toggleAndGet() is a more natural to concurrent algorithms that are based
>> on a toggle bit. E.g. diffracting tree, counting network & etc.
>>
>> Regards,
>> Guy Korland
>> @g_korland
>>
>> On Fri, Feb 12, 2010 at 6:19 PM, Martin Buchholz <martinrb at google.com
>> <mailto:martinrb at google.com>> wrote:
>>
>>    AtomicBoolean.toggleAndGet() does seem plausible,
>>    although no obvious use case occurs to me.
>>    But it doesn't buy you much.
>>    Besides a cas loop on AtomicBoolean,
>>    you could also use
>>
>>    (atomicInteger.incrementAndGet() & 0x1) == 0
>>
>>    so without more motivation
>>    I'd be inclined to leave it out.
>>
>>    Martin
>>
>>    On Fri, Feb 12, 2010 at 06:59, Guy Korland <gkorland at gmail.com
>>    <mailto:gkorland at gmail.com>> wrote:
>>    >  Hi,
>>    >  AtomicBoolean.toggleAndGet() and getAndToggle()
>>    >  It seems like a very usable feature to add to AtomicBoolean and
>>    something
>>    >  that matchs the other AtomicX classes API (incrementAndGet).
>>    >  Is there any reason why this class was discriminated?
>>
>>
>>
>>
>> _______________________________________________
>> Concurrency-interest mailing list
>> Concurrency-interest at cs.oswego.edu
>> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>



More information about the Concurrency-interest mailing list