mirror of
https://github.com/13hannes11/UU_la_parallel_programming_practical_assignments.git
synced 2024-09-04 00:50:58 +02:00
first working threaded implementation of adt_checking
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 )
|
||||
add_library(sets STATIC ADT_Stack.h ADT_Stack.cpp Node.h Node.cpp Stack.h Stack.cpp SimpleLockingStack.h SimpleLockingStack.cpp)
|
||||
|
||||
26
Assignment_3/lib/SimpleLockingStack.cpp
Normal file
26
Assignment_3/lib/SimpleLockingStack.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "SimpleLockingStack.h"
|
||||
#include "ADT_Stack.h"
|
||||
|
||||
SimpleLockingStack::SimpleLockingStack(ADTOperationQueue * queue) : Stack(queue) { }
|
||||
|
||||
void SimpleLockingStack::push(int element) {
|
||||
mut.lock();
|
||||
vec.push_back(element);
|
||||
op_queue->enqueue(create_operation(methodname::push, element));
|
||||
mut.unlock();
|
||||
}
|
||||
int SimpleLockingStack::pop() {
|
||||
mut.lock();
|
||||
int e = vec.back();
|
||||
vec.pop_back();
|
||||
op_queue->enqueue(create_operation(methodname::pop, e));
|
||||
mut.unlock();
|
||||
return e;
|
||||
}
|
||||
int SimpleLockingStack::size() {
|
||||
mut.lock();
|
||||
int s = vec.size();
|
||||
op_queue->enqueue(create_operation(methodname::size, s));
|
||||
mut.unlock();
|
||||
return s;
|
||||
}
|
||||
16
Assignment_3/lib/SimpleLockingStack.h
Normal file
16
Assignment_3/lib/SimpleLockingStack.h
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
|
||||
#include "Stack.h"
|
||||
|
||||
class SimpleLockingStack:public Stack{
|
||||
private:
|
||||
std::vector<int> vec;
|
||||
std::mutex mut;
|
||||
public:
|
||||
SimpleLockingStack(ADTOperationQueue * queue);
|
||||
void push(int element);
|
||||
int pop();
|
||||
int size();
|
||||
};
|
||||
@@ -1,9 +1,11 @@
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <thread>
|
||||
|
||||
#include <lib/ADT_Stack.h>
|
||||
#include <lib/Stack.cpp>
|
||||
#include <lib/SimpleLockingStack.h>
|
||||
|
||||
using namespace std::chrono;
|
||||
|
||||
@@ -18,6 +20,10 @@ using namespace std::chrono;
|
||||
#define OP_COUNT 100000
|
||||
#define THREADS 4
|
||||
|
||||
int done_count = 0;
|
||||
std::mutex done_mutex;
|
||||
ADTOperationQueue * op_queue = new ADTOperationQueue();
|
||||
|
||||
std::vector<operation> generate_operations_uniform(){
|
||||
operation default_operation = create_operation(methodname::push, 0);
|
||||
std::vector<operation> operations(OP_COUNT, default_operation);
|
||||
@@ -44,10 +50,11 @@ std::vector<operation> generate_operations_uniform(){
|
||||
return operations;
|
||||
}
|
||||
|
||||
void run_worker(std::vector<operation>* operations, Stack* stack) {
|
||||
void run_worker(Stack* stack) {
|
||||
std::vector<operation> operations = generate_operations_uniform();
|
||||
std::cout << "Worker: start" << std::endl;
|
||||
long op_counter = 0;
|
||||
for (operation op : *operations) {
|
||||
for (operation op : operations) {
|
||||
switch (op.method) {
|
||||
case methodname::pop:
|
||||
stack->pop();
|
||||
@@ -60,20 +67,21 @@ void run_worker(std::vector<operation>* operations, Stack* stack) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Worker: done" << std::endl;
|
||||
done_mutex.lock();
|
||||
done_count ++;
|
||||
done_mutex.unlock();
|
||||
std::cout << "Worker: done" << std::endl;
|
||||
}
|
||||
|
||||
void run_checker(ADTOperationQueue queue, int* done_count) {
|
||||
void run_checker() {
|
||||
ADTStack * adt_stack = new ADTStack();
|
||||
while(true) {
|
||||
int finished = *done_count;
|
||||
size_t queue_size = queue.size();
|
||||
size_t queue_size = op_queue->size();
|
||||
|
||||
if (finished >= THREADS && queue_size == 0) {
|
||||
if (done_count >= THREADS && queue_size == 0) {
|
||||
break;
|
||||
} else {
|
||||
operation op = queue.dequeue();
|
||||
} else if(queue_size != 0){
|
||||
operation op = op_queue->dequeue();
|
||||
adt_stack->do_op(&op);
|
||||
}
|
||||
}
|
||||
@@ -81,5 +89,20 @@ void run_checker(ADTOperationQueue queue, int* done_count) {
|
||||
}
|
||||
|
||||
int main(){
|
||||
Stack * s = new SimpleLockingStack(op_queue);
|
||||
|
||||
std::thread *worker= new std::thread[THREADS];
|
||||
|
||||
for (int i=0;i<THREADS;i++){
|
||||
worker[i]=std::thread(run_worker, s);
|
||||
}
|
||||
|
||||
std::thread monitor = std::thread(run_checker);
|
||||
|
||||
for (int i=0;i<THREADS;i++){
|
||||
worker[i].join();
|
||||
}
|
||||
|
||||
monitor.join();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user