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:
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;
|
||||
}
|
||||
Reference in New Issue
Block a user