pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/SuboptimalEng/cpp-tutorials/commit/e6b5765b5d69abbbaa4c8eaa4ac1b52471e5ca46

ossorigen="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/code-cbf357275a7402ca.css" /> Add code for 38th tutorial on thread mutex. · SuboptimalEng/cpp-tutorials@e6b5765 · GitHub
Skip to content

Commit e6b5765

Browse files
committed
Add code for 38th tutorial on thread mutex.
1 parent 82a2f80 commit e6b5765

3 files changed

Lines changed: 70 additions & 0 deletions

File tree

038-threads-mutex/Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
all:
2+
clang++ -std=c++17 -g -O0 main.cpp -o main
3+
4+
debug:
5+
clang++ -std=c++17 -g -O0 main.cpp -o main && lldb ./main
6+
7+
run:
8+
clang++ -std=c++17 -g -O0 main.cpp -o main && ./main
9+
10+
clean:
11+
rm -rf main *.dSYM *.s

038-threads-mutex/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Video Overview
2+
3+
- Previously, we talked about threads
4+
- What is a mutex?
5+
- Mutual exclusion (venn diagram, where the 2 circles don't meet)
6+
- Why do we need mutex?
7+
- Prevent race conditions + data corruption
8+
- Show how threads can overwrite data
9+
- How does mutex fix it?
10+
- Lock/unlock outside the for-loop
11+
- Why you need to be careful with mutexes?
12+
- Lock/unlock inside the for-loop
13+
14+
## Titles
15+
16+
- What is a C++ Mutex and How Does it?
17+
18+
## References
19+
20+
- ChatGPT and Gemini

038-threads-mutex/main.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// #include <chrono>
2+
#include <iostream>
3+
#include <mutex>
4+
#include <thread>
5+
6+
using namespace std;
7+
8+
std::mutex counter_mtx;
9+
int counter = 0;
10+
11+
void increment_counter() {
12+
counter_mtx.lock();
13+
for (size_t i = 0; i < 1000000; i++) {
14+
counter++;
15+
}
16+
counter_mtx.unlock();
17+
}
18+
19+
int main() {
20+
// cout << "start count: " << counter << endl;
21+
// std::thread t1(increment_counter);
22+
// std::thread t2(increment_counter);
23+
// t1.join();
24+
// t2.join();
25+
// cout << "end count: " << counter << endl;
26+
27+
cout << "start count: " << counter << endl;
28+
auto start = std::chrono::high_resolution_clock::now();
29+
std::thread t1(increment_counter);
30+
std::thread t2(increment_counter);
31+
t1.join();
32+
t2.join();
33+
auto end = std::chrono::high_resolution_clock::now();
34+
cout << "end count: " << counter << endl;
35+
std::chrono::duration<double, std::milli> elapsed = end - start;
36+
cout << "Function took: " << elapsed.count() << " ms" << endl;
37+
38+
return 0;
39+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy