mirror of
https://github.com/13hannes11/UU_la_parallel_programming_practical_assignments.git
synced 2024-09-04 00:50:58 +02:00
implement pop and push for treiber stack
This commit is contained in:
@@ -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)
|
||||
|
||||
40
Assignment_3/lib/TreiberStack.cpp
Normal file
40
Assignment_3/lib/TreiberStack.cpp
Normal 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;
|
||||
}
|
||||
14
Assignment_3/lib/TreiberStack.h
Normal file
14
Assignment_3/lib/TreiberStack.h
Normal 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();
|
||||
};
|
||||
Reference in New Issue
Block a user