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

@@ -1 +1 @@
add_library(sets STATIC ADT_Stack.h ADT_Stack.cpp Node.h Node.cpp Stack.h Stack.cpp SimpleLockingStack.h SimpleLockingStack.cpp) add_library(sets STATIC ADT_Stack.h ADT_Stack.cpp Node.h Node.cpp Stack.h Stack.cpp SimpleLockingStack.h SimpleLockingStack.cpp TreiberStack.h TreiberStack.cpp)

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;
}

View File

@@ -0,0 +1,14 @@
#include <atomic>
#include "Stack.h"
#include "Node.h"
class TreiberStack:public Stack{
private:
std::atomic<Node *> top;
public:
TreiberStack(ADTOperationQueue * queue);
void push(int element);
int pop();
int size();
};