-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
84 lines (70 loc) · 2.35 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <thread>
#include <mutex>
#include <atomic>
#include <chrono>
#include <vector>
#include <mutex>
#include <cstdlib>
//This minimum compiler version to compile this solution is c++20
using std::cout;
using std::endl;
const size_t CPUThreadNum = std::thread::hardware_concurrency(); // The device's maximum thread capacity
std::mutex mtx;
class ExpensiveObject {
public:
ExpensiveObject() = default;
void doSomething() {
std::unique_lock<std::mutex> lock(mtx);
std::this_thread::sleep_for(std::chrono::milliseconds(5));
cout << "ExpensiveObject is doing something" << endl;
std::this_thread::sleep_for(std::chrono::milliseconds(5));
lock.unlock();
}
~ExpensiveObject() = default;
};
class LazyInitialization {
private:
ExpensiveObject* object = nullptr;
std::once_flag flag; // std::once_flag to ensure initialization happens only once
std::atomic<bool> ready{ false }; // Atomic flag to indicate whether the object is initialized
void initializeObject() {
if (!object) {
object = new ExpensiveObject(); // Initialize the object
ready.store(true, std::memory_order_release); // Mark object as ready
}
}
public:
void useObject() {
std::call_once(flag, &LazyInitialization::initializeObject, this); // Call initializeObject() once
while (!ready.load(std::memory_order_acquire)) {} // Wait until object is initialized with acquire semantics
object->doSomething(); // Use the object
}
~LazyInitialization() {
delete object;
}
};
int main() {
LazyInitialization test1;
std::vector<std::jthread> vec;
//creating a vector of thread based on the device capacity minus 1
for (size_t i = 1; i < CPUThreadNum; i++) {
vec.emplace_back([&test1]() {test1.useObject(); });
}
//jthread object is auto joinable but for double safty implementing join function
for (auto& v : vec) {
v.join();
}
// Simulate multiple threads trying to use the object simultaneously
std::jthread t1([&test1]() {
test1.useObject();
});
std::jthread t2([&test1]() {
test1.useObject();
});
t1.join();
t2.join();
cout << "\nThe maximum thread capacity on this devise is: " << CPUThreadNum << endl;
std::system("pause");
return 0;
}