implement pop and push for treiber stack

This commit is contained in:
2021-01-10 21:28:35 +01:00
parent 165fb8676b
commit 02baf6cb28
3 changed files with 55 additions and 1 deletions

View File

@@ -0,0 +1,40 @@
#include<stdexcept>
#include "TreiberStack.h"
#include "ADT_Stack.h"
TreiberStack::TreiberStack(ADTOperationQueue * queue) : Stack(queue) { }
void TreiberStack::push(int element) {
Node * n;
Node * t;
while (true) {
t = top.load(std::memory_order_relaxed);
n->next = t;
if (top.compare_exchange_weak(t, n)) {
op_queue->enqueue(create_operation(methodname::push, element));
return;
}
}
}
int TreiberStack::pop() {
Node * n;
Node * t;
while (true) {
t = top.load(std::memory_order_relaxed);
if (t == NULL) {
throw std::invalid_argument("Invalid operation: pop on an empty stack");
} else if (top.compare_exchange_weak(t, t->next)) {
int data = t->get_data();
op_queue->enqueue(create_operation(methodname::pop, data));
return data;
}
}
}
int TreiberStack::size() {
return 0;
}