mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 12:44:40 +03:00
33 lines
634 B
C++
33 lines
634 B
C++
//
|
|
// 7.2.critical.section.a.cpp
|
|
// chapter 7 parallelism and concurrency
|
|
// modern c++ tutorial
|
|
//
|
|
// created by changkun at changkun.de
|
|
// https://github.com/changkun/modern-cpp-tutorial
|
|
//
|
|
|
|
#include <iostream>
|
|
#include <mutex>
|
|
#include <thread>
|
|
|
|
int v = 1;
|
|
|
|
void critical_section(int change_v) {
|
|
static std::mutex mtx;
|
|
std::lock_guard<std::mutex> lock(mtx);
|
|
|
|
// do contention operations
|
|
v = change_v;
|
|
|
|
// mtx will be destructed when exit this region
|
|
}
|
|
|
|
int main() {
|
|
std::thread t1(critical_section, 2), t2(critical_section, 3);
|
|
t1.join();
|
|
t2.join();
|
|
|
|
std::cout << v << std::endl;
|
|
return 0;
|
|
} |