mercredi 24 avril 2019

Do spinlocks guarantee order of acquisition?

I have a simple spinlock implementation similar to this:

class Spinlock{
  std::atomic_flag flag;
public:
  Spinlock(): flag(ATOMIC_FLAG_INIT) {}
  ~Spinlock() {}

  void lock(){
    while(flag.test_and_set(std::memory_order_acquire));
  }

  void unlock(){
    flag.clear(std::memory_order_release);
  }
};

My question is similar to this one on mutexes but for spinlocks:

  • Thread 1 calls lock()
  • Before Thread 1 calls unlock(), Thread 2 and 3 both call lock().

Is it guaranteed that Thread 2 will acquire the spinlock before Thread 3?

Aucun commentaire:

Enregistrer un commentaire