mirror of
https://github.com/13hannes11/UU_la_parallel_programming_practical_assignments.git
synced 2024-09-04 00:50:58 +02:00
refactored FineList to have Node in seperate file
This commit is contained in:
@@ -1,46 +1,26 @@
|
|||||||
#include<iostream>
|
#include <bits/stdc++.h>
|
||||||
#include<thread>
|
|
||||||
#include <algorithm>
|
#include"Node.cpp"
|
||||||
#include <queue>
|
|
||||||
#include <mutex>
|
|
||||||
#include <assert.h>
|
|
||||||
#include <bits/stdc++.h>
|
|
||||||
|
|
||||||
// TODO: implement fine grained locking
|
// TODO: implement fine grained locking
|
||||||
|
|
||||||
class Node {
|
class FineSet {
|
||||||
public:
|
|
||||||
int data;
|
|
||||||
Node* next;
|
|
||||||
std::mutex mut;
|
|
||||||
public:
|
|
||||||
void lock();
|
|
||||||
void unlock();
|
|
||||||
};
|
|
||||||
void Node::lock() {
|
|
||||||
this->mut.lock();
|
|
||||||
}
|
|
||||||
void Node::unlock() {
|
|
||||||
this->mut.unlock();
|
|
||||||
}
|
|
||||||
|
|
||||||
class Set {
|
|
||||||
Node* first;
|
Node* first;
|
||||||
public:
|
public:
|
||||||
Set();
|
FineSet();
|
||||||
bool add(int element);
|
bool add(int element);
|
||||||
bool rmv(int element);
|
bool rmv(int element);
|
||||||
bool ctn(int element);
|
bool ctn(int element);
|
||||||
};
|
};
|
||||||
|
|
||||||
Set::Set(){
|
FineSet::FineSet(){
|
||||||
first = new Node(); // dummy node;
|
first = new Node(); // dummy node;
|
||||||
Node* last = new Node();
|
Node* last = new Node();
|
||||||
last->data = INT_MAX; // end node;
|
last->data = INT_MAX; // end node;
|
||||||
first->next = last;
|
first->next = last;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Set::add(int element) {
|
bool FineSet::add(int element) {
|
||||||
this->first->lock();
|
this->first->lock();
|
||||||
Node* p = this->first;
|
Node* p = this->first;
|
||||||
Node* c = p->next;
|
Node* c = p->next;
|
||||||
@@ -68,7 +48,7 @@ bool Set::add(int element) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Set::rmv(int element) {
|
bool FineSet::rmv(int element) {
|
||||||
this->first->lock();
|
this->first->lock();
|
||||||
Node* p = this->first;
|
Node* p = this->first;
|
||||||
Node* c = p->next;
|
Node* c = p->next;
|
||||||
@@ -92,7 +72,7 @@ bool Set::rmv(int element) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bool Set::ctn(int element) {
|
bool FineSet::ctn(int element) {
|
||||||
this->first->lock();
|
this->first->lock();
|
||||||
Node* p = this->first;
|
Node* p = this->first;
|
||||||
Node* c = p->next;
|
Node* c = p->next;
|
||||||
|
|||||||
17
Assignment_2/Node.cpp
Normal file
17
Assignment_2/Node.cpp
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#include <mutex>
|
||||||
|
|
||||||
|
class Node {
|
||||||
|
public:
|
||||||
|
int data;
|
||||||
|
Node* next;
|
||||||
|
std::mutex mut;
|
||||||
|
public:
|
||||||
|
void lock();
|
||||||
|
void unlock();
|
||||||
|
};
|
||||||
|
void Node::lock() {
|
||||||
|
this->mut.lock();
|
||||||
|
}
|
||||||
|
void Node::unlock() {
|
||||||
|
this->mut.unlock();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user