jeudi 14 mai 2015

Compare some atomic exchange other

what I need to do today with atomic operations is rather tricky and I'd like some help. In a nutshell, I would like to perform the following operation atomically:

/* begin of atomicity */
if (--counter == 0)
{
   Widget* widget = nullptr;

   swap(some_widget, widget);

   /* end of atomicity */

   // use local 'widget' here
}
/* end of atomicity */

I am aware of C++11 atomics and intrinsics. The previous code is only pseudo and is intended to show what I would like to happen atomically. I know one solution would be to make counter have a "Transition" state (example 0xFFFFFFFF) and only change its value moving to this transition first and then to a new value: it would essentially be analogous to a spinlock. Any idea on how to do this efficiently (i.e. no spinlocks, no mutexes, and possibly no waits)?

Thank you very much.

Aucun commentaire:

Enregistrer un commentaire