-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLockFreeProgramming01.cpp
More file actions
52 lines (36 loc) · 1.52 KB
/
LockFreeProgramming01.cpp
File metadata and controls
52 lines (36 loc) · 1.52 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
// ===========================================================================
// LockFreeProgramming01.cpp // Lock-Free Programming
// ===========================================================================
#include <atomic>
#include <print>
namespace LockFreeProgramming {
static void test_lock_free_programming_01()
{
std::atomic<int> value{ 123 };
int expectedValue = 123;
std::println("Previous expected value: {}", expectedValue);
bool b = value.compare_exchange_weak(expectedValue, 456);
std::println("Return Value: {}", b);
std::println("Current expected Value: {}", expectedValue);
std::println("Current Value: {}", value.load());
}
static void test_lock_free_programming_02()
{
std::atomic<int> value{ 123 };
int expectedValue = 124;
std::println("Previous expected value: {}", expectedValue);
bool b = value.compare_exchange_weak(expectedValue, 456);
std::println("Return Value: {}", b);
std::println("Current expected Value: {}", expectedValue);
std::println("Current Value: {}", value.load());
std::println();
}
}
void test_lock_free_programming() {
using namespace LockFreeProgramming;
test_lock_free_programming_01();
test_lock_free_programming_02();
}
// ===========================================================================
// End-of-File
// ===========================================================================