-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathThreadLocalStorage.cpp
More file actions
50 lines (36 loc) · 1.33 KB
/
ThreadLocalStorage.cpp
File metadata and controls
50 lines (36 loc) · 1.33 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
// ===========================================================================
// ThreadLocalStorage.cpp // Thread Local Storage
// ===========================================================================
#include <mutex>
#include <print>
#include <thread>
namespace ThreadLocalStorage {
// =======================================================================
std::mutex mutex{};
extern thread_local int x;
static void function()
{
thread_local int y{};
{
std::lock_guard<std::mutex> guard{ mutex };
x++;
y++;
std::println("TID: {} ", std::this_thread::get_id());
std::println(" &x: {:#010x} => {}", reinterpret_cast<intptr_t>(&x), x);
std::println(" &y: {:#010x} => {}", reinterpret_cast<intptr_t>(&y), y);
}
}
}
void test_thread_local_storage() {
using namespace ThreadLocalStorage;
std::println("Test: {} ", std::this_thread::get_id());
std::println(" &x: {:#010x} => {}", reinterpret_cast<intptr_t>(&x), x);
function();
std::jthread worker1{ function };
function();
std::jthread worker2{ function };
function();
}
// ===========================================================================
// End-of-File
// ===========================================================================