-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_coarse.cpp
60 lines (53 loc) · 1.09 KB
/
list_coarse.cpp
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
#include <stdexcept>
#include "benchmark.hpp"
struct node {
data val;
node *next;
};
class List {
public:
spinlock lock;
node *head;
List(): head(nullptr) {}
~List() {
while (head != nullptr) {
node *next = head->next;
delete head;
head = next;
}
}
void push(data item) {
node *newitem = new node;
newitem->val = item;
lock.lock();
newitem->next = head;
head = newitem;
lock.unlock();
}
data pop() {
lock.lock();
if (head == nullptr) {
lock.unlock();
throw std::runtime_error("Can't pop from an empty list");
}
data ret = head->val;
node *oldhead = head;
head = head->next;
delete oldhead;
lock.unlock();
return ret;
}
data sum() {
data s = 0;
lock.lock();
for (node *n = head; n != nullptr; n = n->next) {
s += n->val;
}
lock.unlock();
return s;
}
};
int main() {
List l;
benchmark_list(l);
}