-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlock-based_linkedlist.cpp
More file actions
86 lines (72 loc) · 2.54 KB
/
lock-based_linkedlist.cpp
File metadata and controls
86 lines (72 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <thread>
struct Node{
int value;
Node* next;
};
class LinkedList{
private:
Node* head;
std::mutex m_lock;
public:
LinkedList(){
head = nullptr;
}
void insert(int value){
Node* temp = (Node*)malloc(sizeof(Node));
temp->value = value;
std::lock_guard<std::mutex> mylock(m_lock); // lock_guard used to handle RAII itself
temp->next = head;
head = temp;
}
int lookup(int value){
std::lock_guard<std::mutex> mylock(m_lock);
Node* temp = head;
while(temp!=nullptr){
if(temp->value == value) return 1;
temp = temp->next;
}
return 0;
}
void print(){
Node* temp = head;
int count = 0;
while(temp!=nullptr){
std::cout<<temp->value<<" ";
temp = temp->next;
count++;
}
std::cout<<std::endl<<"count:"<<count<<std::endl;
}
~LinkedList(){
Node* curr = head;
while(curr!=nullptr){
Node* next = curr->next;
free(curr); // with malloc use free, with new use delete
// malloc does not call constructors and free does not call destructors
curr = next;
}
}
};
void func(LinkedList &list, int id){
for(int i=0;i<100000;i++){
list.insert(10*id + i);
}
}
int main(){
LinkedList list;
std::thread t1(func, std::ref(list), 1); // std::ref is used because we want same linkedlist to pass in all threads and mutex is not copyable, constructs a reference wrapper
// std::thread is different from normal function call, thread always copies/moves arguments that's why std::ref is needed, std::ref explicitly tells compiler the intentions of developer that it is fine.
// Read:: https://stackoverflow.com/questions/34078208/passing-object-by-reference-to-stdthread-in-c11
// Read:: https://stackoverflow.com/questions/73834500/stdthread-constructor-passing-a-value-by-reference-needs-to-call-ref-why
// Crux: The variable can go out of scope from main thread leading to undefined behaviour, so compiler prevents that by throwing error, writing std::ref explicitly tells compiler that it is what we want.
std::thread t2(func, std::ref(list), 2);
std::thread t3(func, std::ref(list), 3);
t1.join();
t2.join();
t3.join();
list.print();
return 0;
}
// can be scaled by using lock for each node. often less performant that above.
// can try mixture of both that lock for fixed length of linked list