c++标准库(第二版,作者_nicolaimjosuttis)_第18章并发_18.1async1_superfreak的博客-爱代码爱编程
对于初学者而言,“以多线程运行程序”的最佳起点就是C++标准库中由std::sync()和class std::future<>提供的高级接口:
- async()提供一个接口,让一段功能或者说一个callable object 若是可能的话在后台运行,成为一个独立的线程。
- Class future<>允许你等待线程结束并获取其结果(一个返回值或者一个异常)
下面的这个例子是需要计算两个操作数的和,而这两个数是两个函数的返回值。
// async1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <future>
#include <thread>
#include <chrono>
#include <random>
#include <iostream>
#include <exception>
using namespace std;
int doSomething(char c)
{
//random-number generator(use C as seed to get different sequence)
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()
{
std::cout << "starting func1() in background" << " and func2() in foreground:" << std::endl;
//start func1() asynchronously(now or later or never)
std::future<int> result1(std::async(func1));
int result2 = func2();//call func2() synchronously (here and now )
//print result(wait for func1() to finish and add its result to result2)
int result = result1.get() + result2;
std::cout << "\nresult of func1()+func2(): " << result << std::endl;
}
这里啊读者先不用去纠结doSomething()函数的 具体实现,你就知道它做点事情返回int 就可以了。
传统的实现就是int result=func1()+func2();现在需要用多线程的方式来并行处理,就可以这么写:
//start func1() asynchronously(now or later or never)
std::future<int> result1(std::async(func1));
int result2 = func2();//call func2() synchronously (here and now )
//print result(wait for func1() to finish and add its result to result2)
int result = result1.get() + result2;
在这里async()尝试将其所获得的函数立刻异步启动于一个分离的线程内。因此概念上func1() 在这里被启动了,不会造成main()的停滞。基于两个原因,返回future object 是必要的:
- 它允许你取得传给async()的那个函数的未来结果--也许是个返回值,也许是异常。
- 它必须存在,确保目标函数或快、或慢最终会被调用。
也可以用auto(并且是推荐用auto)来声明future object :
auto result1(std::async(func1));
接下来启动func2()于前台,这个就是正常的同步化的调用,于是程序在此停滞:
int result2=func2();
如果先前func1()成功地被async()启动并且尚未结束,现在func1()和func2()就是并行运作。
接下来处理总和。这就是需要func1()结果的时刻。为了获得它,需要使用get()
int result = result1.get() + result2;
随着get()被调用,会发生下面三件事情当中的一个:
- 如果func1()被async()启动于一个分离线程中并且已经结束,你会立刻获得其结果。
- 如果func1()被async()启动但是并没有运行结束,get()会引发停滞(block),待func1()结束后获得结果。
- 如果func1()没有启动,会强迫启动如同一个同步调用;get()会引发停滞(block)直至产生结果。
调用async()并不保证传入的函数一定会被启动和结束。
这个程序在我的机器上的运行结果是:
starting func1() in background and func2() in foreground:
.++...+...+..++.++++
result of func1()+func2(): 89