-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpp2_10.html
More file actions
85 lines (73 loc) · 3.11 KB
/
Copy pathCpp2_10.html
File metadata and controls
85 lines (73 loc) · 3.11 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
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
85
<article>
<h1>C++ II: Page 10 - Advanced Concepts: Growth Path</h1>
<section>
<h2>The Expert Road</h2>
<p>To transition from a competent coder to a C++ expert, you must look under the hood. Master template metaprogramming, understand memory alignment, and embrace concurrency.</p>
<h3>The Code implementation</h3>
<pre><code class="language-cpp">/*
* File: advanced.cpp
* Demonstration of advanced C++: Template Metaprogramming and Concurrency
*/
#include <iostream>
#include <thread>
#include <mutex>
#include <vector>
#include <type_traits>
using namespace std;
// 1. Template Metaprogramming (Compile-time computation)
template <int N>
struct Factorial {
static const int value = N * Factorial<N - 1>::value;
};
template <>
struct Factorial<0> {
static const int value = 1;
};
// 2. Concurrency with Mutex and LockGuard
mutex mtx;
void printSafe(int id) {
lock_guard<mutex> lock(mtx);
cout << "Thread " << id << " executing task." << endl;
}
int main() {
// Template usage: computed at compile-time!
cout << "Factorial(5): " << Factorial<5>::value << endl;
// Concurrency usage
vector<thread> threads;
for(int i = 0; i < 3; ++i) {
threads.emplace_back(printSafe, i);
}
for(auto& t : threads) t.join();
// Type traits (compile-time type info)
cout << "Is int integral? " << is_integral<int>::value << endl;
return 0;
}
/*
* Advanced Concepts Nuances:
* 1. Templates: Generic programming; powerful for compile-time code generation.
* 2. Concurrency: std::thread, std::mutex, std::atomic for thread-safe access.
* 3. RAII for Mutex: 'lock_guard' ensures mutex is released, even if exception occurs.
* 4. Memory Alignment: Use 'alignof' to check object alignment, impacting performance.
* 5. Reading assembly: Use 'objdump' or online tools like Compiler Explorer (godbolt.org).
* 6. Template Metaprogramming (TMP) can be used to optimize code by shifting
* runtime computations to compile-time.
* 7. Type Traits: Allow template code to make decisions based on type properties.
*/
</code></pre>
<div class="interactive-sim">
<h3>Concurrency Simulator</h3>
<button class="sim-button" onclick="
const out = document.getElementById('sim-output-cpp2_10');
out.innerHTML = 'thread t = new Thread(task);<br>';
setTimeout(() => out.innerHTML += '>> Thread running concurrently!', 800);
">Run Simulation</button>
<div id="sim-output-cpp2_10" class="sim-output"></div>
</div>
</section>
<footer style="margin-top: 40px; padding: 20px; background: #f8f9fa; border-top: 1px solid #dee2e6;">
<div style="display: flex; justify-content: space-between;">
<a href="#" data-file="Cpp2_9.html" style="text-decoration: none; color: #6c757d;">← Previous: Testing: Unit Testing</a>
<span style="color: #28a745; font-weight: bold;">C++ Masterclass Complete!</span>
</div>
</footer>
</article>