-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpp1_10.html
More file actions
96 lines (83 loc) · 3.47 KB
/
Copy pathCpp1_10.html
File metadata and controls
96 lines (83 loc) · 3.47 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
86
87
88
89
90
91
92
93
94
95
96
<article>
<h1>C++ I: Page 10 - RAII & Destructors</h1>
<section>
<h2>RAII: Resource Acquisition Is Initialization</h2>
<p>RAII is the cornerstone of C++ memory management. It dictates that resources (memory, file handles, network sockets) are acquired in the constructor and released in the destructor. This ensures resource safety even when exceptions occur.</p>
<h3>The Code implementation</h3>
<pre><code class="language-cpp">/*
* File: raii.cpp
* Demonstration of RAII, Destructors, and LockGuard (Mutex RAII)
*/
#include <iostream>
#include <string>
#include <mutex>
using namespace std;
// Mutex for demo
mutex resourceMtx;
class ResourceManager {
private:
string name;
public:
// Constructor: Acquires resource
ResourceManager(string n) : name(n) {
cout << "Resource " << name << " acquired." << endl;
}
// Destructor: Releases resource
~ResourceManager() {
cout << "Resource " << name << " released." << endl;
}
void doWork() {
cout << "Working with " << name << "..." << endl;
}
};
void scopeDemo() {
// Resource acquired
ResourceManager res("FileHandle");
res.doWork();
// Resource automatically released here!
}
void mutexDemo() {
// lock_guard is an RAII wrapper for mutex
lock_guard<mutex> lock(resourceMtx);
cout << "Resource locked." << endl;
// Mutex automatically unlocked here!
}
int main() {
cout << "--- RAII Demonstration ---" << endl;
scopeDemo();
cout << "--- Mutex RAII Demo ---" << endl;
mutexDemo();
cout << "--- Demonstration finished ---" << endl;
return 0;
}
/*
* RAII Nuances:
* 1. Stack-based objects have deterministic destruction.
* 2. RAII eliminates need for manual 'delete' in simple cases.
* 3. Crucial for Exception Safety: Resources are freed if an exception is thrown.
* 4. Rule of Three/Five: If you define a destructor, you often need
* copy/move constructors and assignment operators.
* 5. Smart pointers (unique_ptr, shared_ptr) are the ultimate expression of RAII.
* 6. LockGuard and similar wrappers use RAII to manage OS-level resources.
*/
</code></pre>
<div class="interactive-sim">
<h3>RAII Simulator</h3>
<button class="sim-button" onclick="
const out = document.getElementById('sim-output-cpp1_10');
out.innerHTML = 'ResourceManager res("File");<br>';
setTimeout(() => out.innerHTML += '>> Work done...<br>>> Resource File released!', 800);
">Run Simulation</button>
<div id="sim-output-cpp1_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="Cpp1_9.html" style="text-decoration: none; color: #6c757d;">← Previous: OOP: Inheritance & Polymorphism</a>
<a href="#" data-file="Cpp2_1.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: STL: Vectors →</a>
</div>
<div style="text-align: center; margin-top: 15px;">
<a href="#" data-file="Cpp2_1.html" style="display: inline-block; padding: 10px 20px; background-color: #007bff; color: white; text-decoration: none; border-radius: 5px; font-weight: bold;">Continue to C++ II: Page 1 →</a>
</div>
</footer>
</article>