mercredi 4 mai 2016

Increment an atomic variable only if non-zero

Let's pretend that I have an atomic variable that is a use count of in-flight threads. Next, let's pretend we want to do something once that count hits zero and thereafter not allow the value to change.

That variable is basically a semaphore that can count as high as say 20 or so.

So, is there a reasonable way with only atomics to increment and decrement this count until we hit zero.

std:atomic<int> counter(0);

void thread1()
{
    if(counter.compareZeroMagic())
    {  
       // Even better if we can signal() on zero the first time too...
       return;
    }

    // The rub is right here - if the count goes to zero here before the fetch_add, an early signal may be sent...

    counter.fetch_add(1);
    // Do stuff
    counter.fetch_sub(1);  // And possibly also signal if zero...
}

I am pretty sure we can compare_exchange this, but I'm drawing a blank at the moment. Yes, I could mutex this, but it is so close to atomic niceness so...

Aucun commentaire:

Enregistrer un commentaire