蓝桥ROS机器人之现代C++学习笔记7.3 期物
Posted zhangrelay
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了蓝桥ROS机器人之现代C++学习笔记7.3 期物相关的知识,希望对你有一定的参考价值。
学习了如下程序:
// future example
#include <iostream> // std::cout
#include <future> // std::async, std::future
#include <chrono> // std::chrono::milliseconds
// a non-optimized way of checking for prime numbers:
bool is_prime (int x)
for (int i=2; i<x; ++i) if (x%i==0) return false;
return true;
int main ()
// call function asynchronously:
std::future<bool> fut = std::async (is_prime,444444443);
// do something while waiting for function to set future:
std::cout << "checking, please wait";
std::chrono::milliseconds span (100);
while (fut.wait_for(span)==std::future_status::timeout)
std::cout << '.' << std::flush;
bool x = fut.get(); // retrieve return value
std::cout << "\\n444444443 " << (x?"is":"is not") << " prime.\\n";
return 0;
#include <iostream>
#include <thread>
#include <future>
int main()
// pack a lambda expression that returns 7 into a std::packaged_task
std::packaged_task<int()> task([]()return 7;);
// get the future of task
std::future<int> result = task.get_future(); // run task in a thread
std::thread(std::move(task)).detach();
std::cout << "waiting...";
result.wait(); // block until future has arrived
// output result
std::cout << "done!" << std:: endl << "future result is " << result.get() << std::endl;
return 0;
(⊙﹏⊙)
(⊙﹏⊙)
以上是关于蓝桥ROS机器人之现代C++学习笔记7.3 期物的主要内容,如果未能解决你的问题,请参考以下文章