Skip to content

Commit 14c84bc

Browse files
author
zhao
committed
add cpp_concurent
1 parent cbf33db commit 14c84bc

20 files changed

+933
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# General
2+
.DS_Store
3+
.AppleDouble
4+
.LSOverride
5+
6+
# Icon must end with two \r
7+
Icon
8+
9+
# Thumbnails
10+
._*
11+
12+
# Files that might appear in the root of a volume
13+
.DocumentRevisions-V100
14+
.fseventsd
15+
.Spotlight-V100
16+
.TemporaryItems
17+
.Trashes
18+
.VolumeIcon.icns
19+
.com.apple.timemachine.donotpresent
20+
21+
# Directories potentially created on remote AFP share
22+
.AppleDB
23+
.AppleDesktop
24+
Network Trash Folder
25+
Temporary Items
26+
.apdisk
27+
28+
# bin
29+
bin
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Source code for [C++ 并发编程](https://paul.pub/cpp-concurrency/)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
rm -rf bin
2+
3+
mkdir build
4+
cd build
5+
6+
cmake ../src/
7+
make
8+
9+
cd ../
10+
mv build/bin ./
11+
rm -rf build/
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
#include <thread>
3+
4+
using namespace std;
5+
6+
void hello() {
7+
cout << "Hello World from new thread." << endl;
8+
}
9+
10+
int main() {
11+
thread t(hello);
12+
t.join();
13+
14+
return 0;
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <iostream>
2+
#include <thread>
3+
4+
using namespace std;
5+
6+
int main() {
7+
thread t([] {
8+
cout << "Hello World from lambda thread." << endl;
9+
});
10+
11+
t.join();
12+
13+
return 0;
14+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <cmath>
2+
#include <chrono>
3+
#include <iostream>
4+
#include <thread>
5+
#include <vector>
6+
7+
using namespace std;
8+
9+
static const int MAX = 10e8;
10+
11+
static mutex gMutex;
12+
13+
static double sum = 0;
14+
15+
void worker(int min, int max) {
16+
for (int i = min; i <= max; i++) {
17+
sum += sqrt(i);
18+
}
19+
}
20+
21+
void serial_task(int min, int max) {
22+
auto start_time = chrono::steady_clock::now();
23+
sum = 0;
24+
worker(0, MAX);
25+
auto end_time = chrono::steady_clock::now();
26+
auto ms = chrono::duration_cast<chrono::milliseconds>(end_time - start_time).count();
27+
cout << "Serail task finish, " << ms << " ms consumed, Result: " << sum << endl;
28+
}
29+
30+
void concurrent_worker(int min, int max) {
31+
{
32+
lock_guard guard(gMutex);
33+
cout << "Thread " << this_thread::get_id() << " work for [" << min << ", " << max << "]" << endl;
34+
}
35+
double cur_sum = 0;
36+
for (int i = min; i <= max; i++) {
37+
cur_sum += sqrt(i);
38+
}
39+
{
40+
lock_guard guard(gMutex);
41+
sum += cur_sum;
42+
}
43+
}
44+
45+
void concurrent_task(int min, int max) {
46+
auto start_time = chrono::steady_clock::now();
47+
48+
unsigned concurrent_count = thread::hardware_concurrency();
49+
cout << "hardware_concurrency: " << concurrent_count << endl;
50+
vector<thread> threads;
51+
min = 0;
52+
sum = 0;
53+
for (int t = 0; t < concurrent_count; t++) {
54+
int range = max / concurrent_count * (t + 1);
55+
threads.push_back(thread(concurrent_worker, min, range));
56+
min = range + 1;
57+
}
58+
for (int i = 0; i < threads.size(); i++) {
59+
threads[i].join();
60+
}
61+
62+
auto end_time = chrono::steady_clock::now();
63+
auto ms = chrono::duration_cast<chrono::milliseconds>(end_time - start_time).count();
64+
cout << "Concurrent task finish, " << ms << " ms consumed, Result: " << sum << endl;
65+
}
66+
67+
int main() {
68+
serial_task(0, MAX);
69+
concurrent_task(0, MAX);
70+
return 0;
71+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <thread>
4+
5+
using namespace std;
6+
7+
void hello(string name) {
8+
cout << "Welcome to " << name << endl;
9+
}
10+
11+
int main() {
12+
thread t(hello, "https://paul.pub");
13+
t.join();
14+
15+
return 0;
16+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <chrono>
2+
#include <ctime>
3+
#include <iomanip>
4+
#include <iostream>
5+
#include <sstream>
6+
#include <thread>
7+
8+
using namespace std;
9+
10+
void print_time() {
11+
auto now = chrono::system_clock::now();
12+
auto in_time_t = chrono::system_clock::to_time_t(now);
13+
14+
std::stringstream ss;
15+
ss << put_time(localtime(&in_time_t), "%Y-%m-%d %X");
16+
cout << "now is: " << ss.str() << endl;
17+
}
18+
19+
void sleep_thread() {
20+
this_thread::sleep_for(chrono::seconds(3));
21+
cout << "[thread-" << this_thread::get_id() << "] is waking up" << endl;
22+
}
23+
24+
void loop_thread() {
25+
for (int i = 0; i < 10; i++) {
26+
cout << "[thread-" << this_thread::get_id() << "] print: " << i << endl;
27+
}
28+
}
29+
30+
int main() {
31+
print_time();
32+
33+
thread t1(sleep_thread);
34+
thread t2(loop_thread);
35+
36+
t1.join();
37+
t2.detach();
38+
39+
print_time();
40+
return 0;
41+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <iostream>
2+
#include <mutex>
3+
#include <thread>
4+
5+
using namespace std;
6+
7+
void init() {
8+
cout << "Initialing..." << endl;
9+
// Do something...
10+
}
11+
12+
void worker(once_flag* flag) {
13+
call_once(*flag, init);
14+
}
15+
16+
int main() {
17+
once_flag flag;
18+
19+
thread t1(worker, &flag);
20+
thread t2(worker, &flag);
21+
thread t3(worker, &flag);
22+
23+
t1.join();
24+
t2.join();
25+
t3.join();
26+
27+
return 0;
28+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <cmath>
2+
#include <chrono>
3+
#include <iostream>
4+
#include <thread>
5+
#include <vector>
6+
7+
using namespace std;
8+
9+
static const int MAX = 10e8;
10+
static double sum = 0;
11+
12+
void worker(int min, int max) {
13+
for (int i = min; i <= max; i++) {
14+
sum += sqrt(i);
15+
}
16+
}
17+
18+
void serial_task(int min, int max) {
19+
auto start_time = chrono::steady_clock::now();
20+
sum = 0;
21+
worker(0, MAX);
22+
auto end_time = chrono::steady_clock::now();
23+
auto ms = chrono::duration_cast<chrono::milliseconds>(end_time - start_time).count();
24+
cout << "Serail task finish, " << ms << " ms consumed, Result: " << sum << endl;
25+
}
26+
27+
void concurrent_task(int min, int max) {
28+
auto start_time = chrono::steady_clock::now();
29+
30+
unsigned concurrent_count = thread::hardware_concurrency();
31+
cout << "hardware_concurrency: " << concurrent_count << endl;
32+
vector<thread> threads;
33+
min = 0;
34+
sum = 0;
35+
for (int t = 0; t < concurrent_count; t++) {
36+
int range = max / concurrent_count * (t + 1);
37+
threads.push_back(thread(worker, min, range));
38+
min = range + 1;
39+
}
40+
for (int i = 0; i < threads.size(); i++) {
41+
threads[i].join();
42+
}
43+
44+
auto end_time = chrono::steady_clock::now();
45+
auto ms = chrono::duration_cast<chrono::milliseconds>(end_time - start_time).count();
46+
cout << "Concurrent task finish, " << ms << " ms consumed, Result: " << sum << endl;
47+
}
48+
49+
int main() {
50+
serial_task(0, MAX);
51+
concurrent_task(0, MAX);
52+
return 0;
53+
}

0 commit comments

Comments
 (0)