mirror of
https://github.com/13hannes11/UU_la_parallel_programming_practical_assignments.git
synced 2024-09-04 00:50:58 +02:00
add fine_list with implemented add_function
This commit is contained in:
76
Assignment_2/FineList.cpp
Normal file
76
Assignment_2/FineList.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
#include<iostream>
|
||||
#include<thread>
|
||||
#include <algorithm>
|
||||
#include <queue>
|
||||
#include <mutex>
|
||||
#include <assert.h>
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
// TODO: implement fine grained locking
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
class Set {
|
||||
Node* first;
|
||||
public:
|
||||
Set();
|
||||
bool add(int element);
|
||||
bool rmv(int element);
|
||||
bool ctn(int element);
|
||||
};
|
||||
|
||||
Set::Set(){
|
||||
first = new Node(); // dummy node;
|
||||
Node* last = new Node();
|
||||
last->data = INT_MAX; // end node;
|
||||
first->next = last;
|
||||
}
|
||||
|
||||
bool Set::add(int element) {
|
||||
this->first->lock();
|
||||
Node* p = this->first;
|
||||
Node* c = p->next;
|
||||
c->lock();
|
||||
while (c->data < element) {
|
||||
p->unlock();
|
||||
p = c;
|
||||
c = c->next;
|
||||
c->lock();
|
||||
|
||||
if (c->data == element) {
|
||||
c->unlock();
|
||||
p->unlock();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Node* n = new Node();
|
||||
n->data = element;
|
||||
n->next = c;
|
||||
p->next = n;
|
||||
|
||||
c->unlock();
|
||||
p->unlock();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Set::rmv(int element) {
|
||||
return false;
|
||||
}
|
||||
bool Set::ctn(int element) {
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user