在linux C++ 开发我们常规的线程创建当中,如果需要获取线程的结果,可能使用的方式是全局变量共享的,来得知最终的结果数据,现在可以使用另外一种方式来处理,即C++11 提供的以下方案,std::future,是一个函数模板。
1、 std::async
std::future 是一个结果集用来返回线程的数据,std::async 可以创建一个线程,并传递参数,通过std::future 中的 get()函数可以获取到数据
#include <mutex>
#include <thread>
#include <future>
int fun(int x) {
x++;
x *= 10;
std::cout <<"thread id:" << std::this_thread::get_id() << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5));
return x;
}
int main()
{
std::future<int> fu = std::async(fun, 1);
std::cout << fu.get() << std::endl;
}
2、std::packaged_task
提前设置好需要打包的函数,并绑定一下结果集,通过std::ref(pt),传递函数,进而使用std::future来获取线程执行结果
#include <mutex>
#include <thread>
#include <future>
int fun(int x) {
x++;
x *= 10;
std::cout <<"thread id:" << std::this_thread::get_id() << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5));
return x;
}
int main()
{
std::packaged_task<int(int)> pt(fun); // 将函数打包起来
std::future<int> fu = pt.get_future(); // 并将结果返回给future
std::thread t(std::ref(pt), 1);
std::cout << fu.get() << std::endl;
std::cout << std::this_thread::get_id() << std::endl;
t.join();
}
3、std::shared_future 和 std::future::valid()
std::future::valid() 是用来判定当前的std::future 是否有效,std::shared_future 创建共享的结果集
#include <mutex>
#include <thread>
#include <future>
int fun(int x) {
x++;
x *= 10;
std::cout <<"thread id:" << std::this_thread::get_id() << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5));
return x;
}
int main()
{
// std::launch::async 立刻创建线程
// std::launch::deferred 当执行到fu.get才开始创建线程
std::future<int> fu = std::async(std::launch::async, fun, 1);
std::future<int> fu2;
fu2 = std::move(fu);
std::shared_future<int> fu3 = fu2.share();
if (fu2.valid()) {
std::cout << "fu is valid" << std::endl;
std::cout << fu2.get() << std::endl;
std::cout << std::this_thread::get_id() << std::endl;
}
else
{
std::cout << "fu is not valid" << std::endl;
}
std::cout << "value: " << fu3.get() << '\n';
std::cout << "its double: " << fu3.get() * 2 << '\n';
}
4、std::future::wait()
std::future::wait() 用来反馈等待结果集的响应是否ready ,
#include <mutex>
#include <thread>
#include <future>
int fun(int x) {
x++;
x *= 10;
std::cout <<"thread id:" << std::this_thread::get_id() << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5));
return x;
}
bool do_check_prime(int x) // 为了体现效果, 该函数故意没有优化.
{
for (int i = 2; i < x; ++i)
if (x % i == 0)
return false;
return true;
}
int main()
{
std::future < bool > fut = std::async(do_check_prime, 194232491);
std::cout << "Checking...\n";
fut.wait();
std::cout << "\n194232491 ";
if (fut.get()) // guaranteed to be ready (and not block) after wait returns
std::cout << "is prime.\n";
else
std::cout << "is not prime.\n";
}
5、std::future::wait_for()
std::future::wait_for() 用来反馈等待结果集的响应是否ready ,设置等待时间
#include <mutex>
#include <thread>
#include <future>
int fun(int x) {
x++;
x *= 10;
std::cout <<"thread id:" << std::this_thread::get_id() << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5));
return x;
}
bool do_check_prime(int x) // 为了体现效果, 该函数故意没有优化.
{
for (int i = 2; i < x; ++i)
if (x % i == 0)
return false;
return true;
}
int main()
{
std::future < bool > fut2 = std::async(do_check_prime, 194232491);
std::cout << "Checking...\n";
std::chrono::milliseconds span(1000); // 设置超时间隔.
// 如果超时,则输出".",继续等待
while (fut2.wait_for(span) == std::future_status::timeout)
std::cout << '.';
std::cout << "\n194232491 ";
if (fut2.get()) // guaranteed to be ready (and not block) after wait returns
std::cout << "is prime.\n";
else
std::cout << "is not prime.\n";
}
由于本人经验有限,如有错误,欢迎修正