c++多线程(十五) - std::shared_future

博客介绍了std::future和std::shared_future两个类模板。std::future对象含线程执行结果,其get()函数含移动语义,只能调用一次;若想多次调用get()获取线程返回值,需使用std::shared_future,它的get()函数含复制语义,可多次调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. std::future

    future是类模板,future对象里面含有线程执行的结果,可以通过调用函数get()来获取结果。但是,get()函数的设计包含移动语义,即只能调用一次,第二次调用时会报异常。

#include<iostream>
#include<future>
using namespace std;

int myThread(int num)
{
	cout << "Thread start id = " << this_thread::get_id() << endl;
	chrono::milliseconds time(5000); //休息5秒
	this_thread::sleep_for(time);
	cout << "Thread end id = " << this_thread::get_id() << endl;
	return num * 2;
}

int main()
{
	cout << "Main start id = " << this_thread::get_id() << endl;
	packaged_task<int(int)> package(myThread);  //把函数myThread通过packaged_task包装起来。
	thread thread(ref(package), 5);
	thread.join();                              //等待线程执行完毕
	future<int> result = package.get_future();  //future对象里保存线程的执行结果	
	cout << "result = " << result.get() << endl;	
	cout << "result = " << result.get() << endl; //第二次调用get()函数,抛出异常
	return 0;
}

    执行结果

 

    调试代码,监视future<int>对象。在语句result.get()执行前

    执行后

    如果想要多次调用get()函数,获取线程的返回值,请使用std::shared_future而不是std::future。

2. std::shared_future

    shared_future也是类模板,其对象里含有线程执行的结果,可以通过函数get()来获取结果。get()函数的设计包含复制语义,可以多次调用。

int main()
{
	cout << "Main start id = " << this_thread::get_id() << endl;
	packaged_task<int(int)> package(myThread);  //把函数myThread通过packaged_task包装起来。
	thread thread(ref(package), 5);
	thread.join();                              //等待线程执行完毕
	shared_future<int> result = package.get_future();  //shared_future对象里保存线程的执行结果	
	cout << "result = " << result.get() << endl;	
	cout << "result = " << result.get() << endl;
	return 0;
}

    执行结果

(gdb) bt #0 0x00005555556112ac in std::__shared_ptr<std::__future_base::_State_baseV2, (__gnu_cxx::_Lock_policy)2>::get() const () #1 0x000055555560e064 in std::__shared_ptr_access<std::__future_base::_State_baseV2, (__gnu_cxx::_Lock_policy)2, false, false>::_M_get() const () #2 0x000055555560b110 in std::__shared_ptr_access<std::__future_base::_State_baseV2, (__gnu_cxx::_Lock_policy)2, false, false>::operator->() const () #3 0x00007ffff48384d4 in std::promise<void*>::set_value (this=0x0, __r=@0x7fffe4ff82f8: 0x7fffdc000c20) at /usr/include/c++/9/future:1114 #4 0x00007ffff483e79f in zkos::soa::servicemethod1::serviceUse2ProxyLogic::MsgsendCommand4 (this=0x7ffff00098e0, Parameter_2=..., Parameter_3=..., promiserPtr=std::shared_ptr<class std::promise<void*>> (empty) = {...}) at /mnt/donglu/skeleton/skeleton_input/sample_structure/CSCBCACore/library/appaswcone/src/logicmanager/serviceuse2proxylogic.cpp:366 #5 0x00007ffff483e050 in zkos::soa::servicemethod1::serviceUse2ProxyLogic::onReceiveMessage (this=0x7ffff00098e0, msg=...) at /mnt/donglu/skeleton/skeleton_input/sample_structure/CSCBCACore/library/appaswcone/src/logicmanager/serviceuse2proxylogic.cpp:300 #6 0x00007ffff47deeaa in aswc::framework::LooperThreadImpl::threadLoop (this=0x7ffff00126a0) at /mnt/donglu/skeleton/skeleton_input/sample_structure/CSCBCACore/library/appaswcone/src/libframework/src/LooperThreadImpl.cpp:146 #7 0x00007ffff482f910 in aswc::utils::Thread::_threadLoop (user=0x7ffff00126a0) at /mnt/donglu/skeleton/skeleton_input/sample_structure/CSCBCACore/library/appaswcone/src/libutils/src/Threads.cpp:695 #8 0x00007ffff482ff8d in thread_data_t::trampoline (t=0x7ffff001dcf0) at /mnt/donglu/skeleton/skeleton_input/sample_structure/CSCBCACore/library/appaswcone/src/libutils/src/Threads.cpp:94 #9 0x00007ffff7dfb609 in start_thread (arg=<optimized out>) at pthread_create.c:477 #10 0x00007ffff7b21353 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
03-28
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值