目录
C/C++Linux服务器开发/后台架构师【零声教育】-学习视频教程-腾讯课堂
C++11异步操作的4个接口
std::future : 异步指向某个任务,然后通过future特性去获取任务函数的返回结果(把一个任务放入线程池运行,然后可以获取返回结果)
std::aysnc: 异步运行某个任务函数
std::packaged_task:将任务和feature绑定在一起的模板,是一种封装对任务的封装(线程池)
std::promise:承诺(线程1设置了某个值,通知另外的线程2)
std::future期待一个返回,从一个异步调用的角度来说,future更像是执行函数的返回值,C++标准库使用std::future为一次性事件建模,如果一个事件需要等待特定的一次性事件,那么这线程可以获取一个future对象来代表这个事件。
异步调用往往不知道何时返回,但是如果异步调用的过程需要同步,或者说后一个异步调用需要使用前一个异步调用的结果。这个时候就要用到future。把future当做异步函数的返回值。
线程可以周期性的在这个future上等待一小段时间,检查future是否已经ready,如果没有,该线程可以先去做另一个任务,一旦future就绪,该future就无法复位(无法再次使用这个future等待这个事件),所以future代表的是一次性事件。
future的类型
在库的头文件中声明了两种future,唯一future(std::future)和共享future(std::shared_future)。
这两个是参照std::unique_ptr和std::shared_ptr设立的,前者的实例是仅有的一个指向其关联事件的实例,而后者可以有多个实例指向同一个关联事件,当事件就绪时,所有指向同一事件的std::shared_future实例会变成就绪。
future的使用
跟thread类似,async允许你通过将额外的参数添加到调用中,来将附加参数传递给函数。如果传入的函数指针是某个类的成员函数,则还需要将类对象指针传入(直接传入,传入指针,或者是std::ref封装)。
默认情况下,std::async是否启动一个新线程,或者在等待future时,任务是否同步运行都取决于你给的参数。这个参数为std::launch类型
- std::launch::async,表明函数会在创建的新线程上运行。
- std::launch::defered表明该函数会被延迟调用,直到在future上调用get()或者wait()为止。
- std::launch::sync = std::launch::defered,表明该函数会被延迟调用
4.std::launch::any = std::launch::defered | std::launch::async,表明该函数会被延迟调用,调用时在新线程上运行
- //future
- #include <iostream>
- #include <future>
- #include <thread>
- using namespace std;
-
- int find_result_to_add()
- {
- std::this_thread::sleep_for(std::chrono::seconds(5)); // 用来测试异步延迟的影响
- std::cout << "find_result_to_add" << std::endl;
- return 1 + 1;
- }
-
- int find_result_to_add2(int a, int b)
- {
- std::this_thread::sleep_for(std::chrono::seconds(5)); // 用来测试异步延迟的影响
- return a + b;
- }
-
- void do_other_things()
- {
- std::cout << "do_other_things" << std::endl;
- // std::this_thread::sleep_for(std::chrono::seconds(5));
- }
-
- int main()
- {
- // std::future<T> std::async是异步线程
- //三种方式
- std::future<int> result = std::async(std::launch::async, find_result_to_add); //表明函数会在自己创建的线程上运行,不会阻塞当前线程
- // std::future<decltype (find_result_to_add())> result = std::async(find_result_to_add);
- // auto result = std::async(find_result_to_add); // 推荐的写法
- do_other_things();
- std::cout << "result: " << result.get() << std::endl; // 延迟是否有影响? 阻塞等待find_result_to_add返回值
-
- //需要传递参数
- // std::future<decltype(find_result_to_add2(int, int))> result2 = std::async(find_result_to_add2, 10, 20); //错误
- std::future<decltype (find_result_to_add2(0, 0))> result2 = std::async(find_result_to_add2, 10, 20);
- // auto result2 = std::async(find_result_to_add2, 10, 20); // 推荐的写法
- std::cout << "result2: " << result2.get() << std::endl; // 延迟是否有影响? 阻塞等待find_result_to_add2返回值
-
- std::cout << "main finish" << endl;
- return 0;
- }


如果说std::async和std::feature还是分开看的关系的话,那么std::packaged_task就是将任务和feature绑定在一起的模板,是一种封装对任务的封装。
可以通过std::packaged_task对象获取任务相关联的feature,调用get_future()方法可以获得std::packaged_task对象绑定的函数的返回值类型的future。std::packaged_task的模板参数是函数签名。
- //-package_task
- #include <iostream>
- #include <future>
- #include <thread>
-
- using namespace std;
-
- int add(int a, int b, int c)
- {
- std::this_thread::sleep_for(std::chrono::seconds(2));
- std::cout << "call add\n";
- return a + b + c;
- }
-
- void do_other_things()
- {
- std::cout << "do_other_things" << std::endl;
- }
-
- int main()
- {
- std::packaged_task<int(int, int, int)> task(add); // 1. 封装任务,还没有运行
- // std::this_thread::sleep_for(std::chrono::seconds(2)); // 用来测试异步延迟的影响
-
- do_other_things();
- std::future<int> result = task.get_future(); // 这里运行吗?这里只是获取 future
- // 这里才真正运行
- task(1, 1, 2); //必须要让任务执行,否则在get()获取future的值时会一直阻塞
- std::cout << "result:" << result.get() << std::endl;
- std::cout << "end" << std::endl;
- return 0;
- }
std::promise提供了一种设置值的方式,它可以在这之后通过相关联的std::future对象进行读取。换种说法,之前已经说过std::future可以读取一个异步函数的返回值了,那么这个std::promise就提供一种方式手动让future就绪。
线程在创建promise的同时会获得一个future,然后将promise传递给设置他的线程,当前线程则持有future,以便随时检查是否可以取值。
- //promise
- // std::promise和std::future配合,可以在线程之间传递数据。
- #include <future>
- #include <string>
- #include <thread>
- #include <iostream>
- using namespace std;
-
- void print1(std::promise<std::string>& p)
- {
- std::cout << "print1 sleep" << std::endl;
- std::this_thread::sleep_for(std::chrono::seconds(1));
- p.set_value("set string"); // 相当于返回future的结果
- }
-
- void print2(std::promise<int>& p)
- {
- std::cout << "print2 sleep" << std::endl;
- p.set_value(1);
- }
-
- void do_some_other_things()
- {
- std::cout << "do_some_other_things" << std::endl;
- }
-
- int main()
- {
- std::cout << "main1 -------------" << std::endl;
- std::promise<std::string> promise; // 注意类型:
-
- std::future<std::string> result = promise.get_future(); // future
-
- std::thread t(print1, std::ref(promise)); // 线程设置 传引用std::ref
- do_some_other_things();
- std::cout << "wait get result" << std::endl;
- std::cout <<"result " << result.get() << std::endl; // 在主线程等待 promise的返回 result set string
- t.join();
-
- std::cout << "\n\nmain2 -------------" << std::endl;
- std::promise<int> promise2;
-
- std::future<int> result2 = promise2.get_future();
- std::thread t2(print2, std::ref(promise2));
- do_some_other_things();
- std::cout << "result2 " << result2.get() << std::endl;
- t2.join();
- return 0;
- }

future的表现为期望,当前线程持有future时,期望从future获取到想要的结果和返回,可以把future当做异步函数的返回值。
promise是一个承诺,当线程创建了promise对象后,这个promise对象向线程承诺他必定会被人设置一个值,和promise相关联的future就是获取其返回的手段。