implement count

This commit is contained in:
2021-01-10 21:32:21 +01:00
parent 02baf6cb28
commit 2e226879ef
2 changed files with 9 additions and 2 deletions

View File

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

View File

@@ -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);