mirror of
https://github.com/13hannes11/UU_la_parallel_programming_practical_assignments.git
synced 2024-09-04 00:50:58 +02:00
implement count
This commit is contained in:
@@ -4,7 +4,9 @@
|
|||||||
#include "ADT_Stack.h"
|
#include "ADT_Stack.h"
|
||||||
|
|
||||||
|
|
||||||
TreiberStack::TreiberStack(ADTOperationQueue * queue) : Stack(queue) { }
|
TreiberStack::TreiberStack(ADTOperationQueue * queue) : Stack(queue) {
|
||||||
|
count.store(0);
|
||||||
|
}
|
||||||
|
|
||||||
void TreiberStack::push(int element) {
|
void TreiberStack::push(int element) {
|
||||||
Node * n;
|
Node * n;
|
||||||
@@ -15,6 +17,7 @@ void TreiberStack::push(int element) {
|
|||||||
n->next = t;
|
n->next = t;
|
||||||
if (top.compare_exchange_weak(t, n)) {
|
if (top.compare_exchange_weak(t, n)) {
|
||||||
op_queue->enqueue(create_operation(methodname::push, element));
|
op_queue->enqueue(create_operation(methodname::push, element));
|
||||||
|
count.fetch_add(1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -31,10 +34,13 @@ int TreiberStack::pop() {
|
|||||||
} else if (top.compare_exchange_weak(t, t->next)) {
|
} else if (top.compare_exchange_weak(t, t->next)) {
|
||||||
int data = t->get_data();
|
int data = t->get_data();
|
||||||
op_queue->enqueue(create_operation(methodname::pop, data));
|
op_queue->enqueue(create_operation(methodname::pop, data));
|
||||||
|
count.fetch_add(-1);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int TreiberStack::size() {
|
int TreiberStack::size() {
|
||||||
return 0;
|
int c = count.load(std::memory_order_relaxed);
|
||||||
|
op_queue->enqueue(create_operation(methodname::size, c));
|
||||||
|
return c;
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
class TreiberStack:public Stack{
|
class TreiberStack:public Stack{
|
||||||
private:
|
private:
|
||||||
|
std::atomic<int> count;
|
||||||
std::atomic<Node *> top;
|
std::atomic<Node *> top;
|
||||||
public:
|
public:
|
||||||
TreiberStack(ADTOperationQueue * queue);
|
TreiberStack(ADTOperationQueue * queue);
|
||||||
|
|||||||
Reference in New Issue
Block a user