Skip to content

Commit

Permalink
add 并发编程
Browse files Browse the repository at this point in the history
  • Loading branch information
liukai234 committed Apr 10, 2020
1 parent 758e518 commit 7d37def
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 0 deletions.
47 changes: 47 additions & 0 deletions 并发编程/joinable_join_detach.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* @Author: your name
* @Date: 2020-04-10 17:55:46
* @LastEditTime: 2020-04-10 19:04:08
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /C++课程学习记录/thread多线程/test.cpp
*/

// join()
/**
* 一旦线程开始,我们要想等待线程完成,需要在该对象上调用join()
* 双重join将导致程序终止
* 在join之前我们应该检查显示是否可以被join,通过使用joinable()
*/

// detach()
/**
* 这用于从父线程分离新创建的线程
* 在分离线程之前,请务必检查它是否可以joinable,
* 否则可能会导致两次分离,并且双重detach()将导致程序终止
* 如果我们有分离的线程并且main函数正在返回,那么分离的线程执行将被挂起
*/

#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>

using namespace std;

void run(int count) {
while (count-- > 0) {
cout << count << endl;
}
std::this_thread::sleep_for(chrono::seconds(3));
}

int main() {
thread t1(run, 10);
cout << "main()" << endl;
if(t1.joinable()) t1.join();
// t1.detach(); // erroe 必须检查是否可以joinable()
if(t1.joinable()) t1.detach();
cout << "main() after" << endl;
return 0;
}
50 changes: 50 additions & 0 deletions 并发编程/mutex并发编程.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* @Author: your name
* @Date: 2020-04-10 19:05:51
* @LastEditTime: 2020-04-10 19:18:25
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /C++课程学习记录/thread多线程/test.cpp
*/

#include <iostream>
#include <mutex>
#include <thread>

using namespace std;

int sum = 0; //shared

mutex m; // 与 lock unlock 成对使用

/*
mutex对象是一个lockable的对象,当关键区域需要被互斥访问的时候被用来当作信号
调用该函数后,调用函数会锁定mutex,在有些情况下调用函数会阻塞。
1、如果mutex当前没有被任何其他线程locked,则调用线程lock这个mutex对象(从此刻到直到其成员函数unlock被调用,当前线程拥有mutex对象)。
2、如果mutex目前被其他线程locked,则当前线程阻塞直到mutex被其他线程unlock。
3、如果mutex目前被当前线程lock,则会产生死锁错误。大部分情况会崩溃,因为mutex不支持递归。
*/

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();
cout << "1\n";
t2.join();
cout << "2\n";

cout << "sum = " << sum << endl;
return 0;
}
File renamed without changes.
57 changes: 57 additions & 0 deletions 并发编程/多线程在函数和lambda算子中.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* @Author: your name
* @Date: 2020-04-10 17:28:24
* @LastEditTime: 2020-04-10 18:50:14
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /C++课程学习记录/thread多线程/intro.cpp
*/

#include <iostream>
#include <thread> // 线程库 编译时要加 -pthread 参数
#include <chrono> // hrono 时钟库,以各种精度跟踪时间的类型的灵活汇集

using namespace std::chrono;
using namespace std;

using ull = unsigned long long;
ull OddSum = 0;
ull EvenSum = 0;

void findEven(ull start, ull end) {
for (ull i = start; i <= end; ++i)
if ((i & 1) == 0)
EvenSum += i;
}

void findOdd(ull start, ull end) {
for (ull i = start; i <= end; ++i)
if ((i & 1) == 1)
OddSum += i;
}

int main() {

ull start = 0, end = 1900000000;

// 引入线程t1, t2
auto startTime = high_resolution_clock::now(); // 高分辨率时钟
// findOdd(start, end); // 寻找奇数
// findEven(start, end); // 寻找偶数

// std::thread t1(findOdd, start, end);
// 或者将t1线程的 定义语句和函数 利用lambda算子合并
std::thread t1([](int start, int end){int i = 0; for(int i = start; i < end; i ++) { if(i & 1 == 1) OddSum += 1; }}, start, end);
t1.join();

std::thread t2(findEven, start, end);
t2.join();

auto stopTime = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stopTime - startTime);

cout << "EvenSum = " << EvenSum << endl;
cout << "OddSum = " << OddSum << endl;
cout << "duration = " << duration.count() / 1000000 << endl;
return 0;
}
45 changes: 45 additions & 0 deletions 并发编程/多线程在类中.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* @Author: your name
* @Date: 2020-04-10 18:02:41
* @LastEditTime: 2020-04-10 18:58:01
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /C++课程学习记录/thread多线程/point.cpp
*/

#include <iostream>
#include <thread>

using namespace std;

// 3.functor (Funciton Object)
class Base {
public:
// 重载operator ()
void operator()(int x) {
while (x-- > 0) {
cout << x << endl;
}
}
// 非静态成员
void fun1(int x) {
while (x-- > 0) {
cout << x << endl;
}
}
// 静态成员
static void fun2(int x) {
while (x-- > 0) {
cout << x << endl;
}
}
};

int main() {
thread t(Base(), 10);
// Base b;
// thread t(&Base::fun1, &b, 10);
// thread t(&Base::fun2, 10);
t.join();
return 0;
}

0 comments on commit 7d37def

Please sign in to comment.