lukasl1991

Lock-free pop-operation

Dec 14th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.44 KB | None | 0 0
  1. template <class T>
  2. bool CASStack<T>::pop(T& ret) {
  3.     node<T>* old_head = head.load(std::memory_order_relaxed);
  4.  
  5.     if(old_head == nullptr) {
  6.         return false;
  7.     }
  8.  
  9.     // from here on we can assume that there is an element to pop
  10.     node<T>* new_head;
  11.  
  12.     do {
  13.         new_head = old_head->next;
  14.     } while(!head.compare_exchange_weak(old_head, new_head, std::memory_order_acquire, std::memory_order_relaxed));
  15.  
  16.     ret = old_head->data;
  17.  
  18.     return true;
  19. }
Add Comment
Please, Sign In to add comment