-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path01_atomic.cpp
More file actions
50 lines (41 loc) · 1.46 KB
/
01_atomic.cpp
File metadata and controls
50 lines (41 loc) · 1.46 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
#include <atomic>
#include <bits/c++config.h>
// =============================================================================
// Example: Lock-Free Queue - Uses atomic integer as exclusive index
// =============================================================================
// std::atomic is used as index to non-atomic memory.
// the fetch_add operation ensures any concurrent push() wont fall into the
// same index.
class atomic_queue {
int queue[100];
std::atomic<std::size_t> front;
void push(int x) {
std::size_t exclusive_index = front.fetch_add(1);
queue[exclusive_index] = x;
}
};
// =============================================================================
// Example: Lock Free List - Uses atomic pointer to non-atomic memory (head)
// =============================================================================
class atomic_list {
struct node {
int value;
node *next;
};
std::atomic<node *> head;
void push_front(int x) {
node *new_n = new node;
new_n->value = x;
// if old_head == head, then do head = new_head;
// otherwise, update old_head.
node *old_head = head;
do {
// keep next reference updated.
new_n->next = old_head;
} while (!head.compare_exchange_strong(old_head, new_n));
}
};
// =============================================================================
// Example:
// =============================================================================
int main() { return 0; }