接下来的篇幅我们将着中讲解 C++ 的并发编程,所有代码内容来自 《C++ 并发编程实战》书籍阅读之后按照自己语言进行总结,输出才是最好的输入。
GitHub - anthonywilliams/ccia_code_samples: Code samples for C++ Concurrency in Action
一、并发和并行的区分
并发指的是多任务之间的切换,实际上是在某一个时刻只有一个任务进程在执行
并行指的是多任务之间同时进行,不存在切换的说法,在某一个时刻可以有多个进程在执行
二、为什么使用并发
主要原因有两个,为了分离关注点和提高性能
分离关注点:在一个程序当中,需要同时执行不同任务,将任务模块化,分离开来,之间通过关联点进行交互,使得每个任务之间可以有效的执行
提高性能:任务的分离,使得任务可以同时运行,不会阻塞在同一线程当中,造成不必要的影响
三、案例讲解
C++ 编写一个简单的hello word 线程,这里面注意的一点是对于线程ID的获取,可以调用std::this_thread::get_id()来获取。
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<thread>
void hello()
{
std::cout << "hello thread id:" << std::this_thread::get_id() << std::endl;
}
void demo()
{
std::thread t(hello);
t.join();
}
int main()
{
std::cout << "main id:" << std::this_thread::get_id() << std::endl;
demo();
}
当然还有之前文章提到的其余方式线程的创建和管理,高级接口async()和future和 promise 用于在线程之间传递参数和处理异常,它与future配对使用。
std::async()
#include <future>
#include <thread>
#include <iostream>
#include <chrono>
#include <random>
#include <exception>
using namespace std;
int DoSomething(char c)
{
// 随机数生成器
std::default_random_engine dre(c);
std::uniform_int_distribution<int> id(10, 1000);
for (int i = 0; i < 10; ++i)
{
this_thread::sleep_for(chrono::milliseconds(id(dre))); // 休眠随机一段时间
cout.put(c).flush();
}
return c;
}
int func1()
{
return DoSomething('.');
}
int func2()
{
return DoSomething('+');
}
int main()
{
cout << "begin..." << endl;
std::future<int> ret1(std::async(func1)); // 异步方式启动func1,它会立即以另一线程启动或者等待
int ret2 = func2(); // 同步方式调用func2
int ret = ret1.get() + ret2; // 得到func1的运行结果并求和
cout << "ret = " << ret << endl;
}
std::promise
#include <thread>
#include <future>
#include <iostream>
#include <string>
#include <exception>
#include <functional>
#include <utility>
using namespace std;
void DoSomething(std::promise<string>& p)
{
try
{
char c = cin.get();
if (c == 'x')
{
throw std::runtime_error(string("char ") + c + " read");
}
string s = string("char ") + c + " processed";
p.set_value(std::move(s));
}
catch(...)
{
p.set_exception(std::current_exception());
}
}
int main()
{
try
{
std::promise<string> p;
std::thread t(DoSomething, std::ref(p));
t.detach();
std::future<string> f(p.get_future());
cout << "ret: " << f.get() << endl;
}
catch(...)
{
cout << "err" << endl;
}
}
由于本人经验有限,如有错误,欢迎修正。