c++ 基础9、c++ 11的并发编程

接下来的篇幅我们将着中讲解 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;
	}
}

由于本人经验有限,如有错误,欢迎修正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值