mirror of
https://github.com/Light-City/CPlusPlusThings.git
synced 2026-02-13 07:25:23 +08:00
update
This commit is contained in:
34
Threading_In_CPlusPlus/4.mutex/critical_section.cpp
Normal file
34
Threading_In_CPlusPlus/4.mutex/critical_section.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// Created by light on 20-2-1.
|
||||
//
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int sum = 0; //shared
|
||||
|
||||
mutex m;
|
||||
|
||||
void *countgold() {
|
||||
int i; //local to each thread
|
||||
for (i = 0; i < 10000000; i++) {
|
||||
m.lock();
|
||||
sum += 1;
|
||||
m.unlock();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main() {
|
||||
thread t1(countgold);
|
||||
thread t2(countgold);
|
||||
|
||||
//Wait for both threads to finish
|
||||
t1.join();
|
||||
t2.join();
|
||||
|
||||
cout << "sum = " << sum << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user