1、async
async: 启动一个异步线程,可以配合future类来获得返回值
#include<iostream>
#include<thread>
#include<future>
using namespace std;
int mythread()
{
cout << "mythread start,id= " << this_thread::get_id() << endl;
this_thread::sleep_for(chrono::milliseconds(1000)); //睡眠1秒
cout << "mythread end,id= " << this_thread::get_id() << endl;
return 5;
}
int main()
{
cout << "async ---test" << endl;
future<int> result = async(mythread);
//async 创建异步线程
//future来绑定关系,获取线程返回值
//其他操作
//.....
cout << "result=" << result.get() << endl << endl; //阻塞在此处,直到获得线程返回结果
}
2、packaged_task
packaged_task: 包装线程函数,可以配合future类来获得返回值
#include<iostream>
#include<thread>
#include<future>
using namespace std;
int mythread()
{
cout << "mythread start,id= " << this_thread::get_id() << endl;
this_thread::sleep_for(chrono::milliseconds(1000)); //睡眠1秒
cout << "mythread end,id= " << this_thread::get_id() << endl;
return 5;
}
int main()
{
cout << "packaged_task ---test" << endl;
packaged_task<int()> mypt(mythread); //把线程函数包装起来
thread t1(std::ref(mypt));
future<int> result = mypt.get_future(); //绑定线程未来返回值
t1.detach();
cout << "result=" << result.get() << endl << endl; //阻塞等待
}
3、promise
promise: 配合future类来实现线程间通信
#include<iostream>
#include<thread>
#include<future>
using namespace std;
void mythread2(promise<int>& tmpp, int calc)
{
cout << "mythread2 start,id= " << this_thread::get_id() << endl;
int res = calc * 2;
this_thread::sleep_for(chrono::milliseconds(2000)); //睡眠2秒
cout << "mythread2 end,id= " << this_thread::get_id() << endl;
tmpp.set_value(res); //赋值,传递到线程外部
return;
}
void mythread3(future<int>& ful)
{
cout << "mythread3 start,id= " << this_thread::get_id() << endl;
int x = ful.get(); //get()会阻塞等待另一个线程中赋值完毕,实现线程间通信
cout << "mythread3_id= " << this_thread::get_id() << " ,ful.get()= " << x << endl;
this_thread::sleep_for(chrono::milliseconds(1000)); //睡眠1秒
cout << "mythread3 end,id= " << this_thread::get_id() << endl;
return;
}
int main()
{
cout << " promise---test" << endl;
promise<int> myprom; //用于线程间通信
thread t2(mythread2, ref(myprom), 10);
//线程间通信
future<int> ful = myprom.get_future(); //绑定
thread t3(mythread3, ref(ful)); //mythread3等待获取参数
t2.join();
t3.join();
//auto result3 = ful.get(); //get只能调用一次
//cout << "result3=" << result3 << endl << endl;
}
4、future_status
future_status: 可以判断线程状态,是否执行完毕
#include<iostream>
#include<thread>
#include<future>
using namespace std;
int mythread()
{
cout << "mythread start,id= " << this_thread::get_id() << endl;
this_thread::sleep_for(chrono::milliseconds(3000)); //睡眠3秒
cout << "mythread end,id= " << this_thread::get_id() << endl;
return 5;
}
int main()
{
future<int> result = async(mythread); //启动异步线程,绑定返回值(result)
//future<int> result = async(launch::deferred,mythread); //此时并不启动,延时启动,result.get()时启动
future_status status = result.wait_for(chrono::seconds(5)); //等5秒后判断result的状态
if (status == future_status::timeout) //超时,wait_for后线程没有结束
{
cout << "线程未执行完" << endl;
}
else if (status == future_status::ready) //线程已执行完毕,返回值已准备好
{
//本例中等待5s,线程只睡眠3s,所以是ready状态
cout << "线程执行完毕" << endl;
cout << "result = " << result.get() << endl;
}
else if (status == future_status::deferred) //线程延时启动,需设置launch::deferred标志位
{
cout << "线程延时执行" << endl;
cout << "result = " << result.get() << endl;
}
}
5、shared_future
shared_future: future.get()是移动语义,只能调用一次,shared_future则可以多次调用get,实现多线程通信
#include<iostream>
#include<thread>
#include<future>
using namespace std;
void mythread2(promise<int>& tmpp, int calc)
{
cout << "mythread2 start,id= " << this_thread::get_id() << endl;
int res = calc * 2;
this_thread::sleep_for(chrono::milliseconds(3000)); //睡眠1秒
cout << "mythread2 end,id= " << this_thread::get_id() << endl;
tmpp.set_value(res); //赋值,传递到线程外部
return;
}
void mythread3(shared_future<int>& ful)
{
cout << "mythread3 start,id= " << this_thread::get_id() << endl;
int x = ful.get(); //get()会阻塞等待另一个线程中赋值完毕,实现线程间通信
cout << "mythread3_id= " << this_thread::get_id() << " ,ful.get()= " << x << endl;
this_thread::sleep_for(chrono::milliseconds(1000)); //睡眠1秒
cout << "mythread3 end,id= " << this_thread::get_id() << endl;
return;
}
void mythread4(shared_future<int>& ful)
{
cout << "mythread4 start,id= " << this_thread::get_id() << endl;
int x = ful.get(); //get()会阻塞等待另一个线程中赋值完毕,实现线程间通信
cout << "mythread4_id= " << this_thread::get_id() << " ,ful.get()= " << x << endl;
this_thread::sleep_for(chrono::milliseconds(1000)); //睡眠1秒
cout << "mythread4 end,id= " << this_thread::get_id() << endl;
return;
}
int main()
{
cout << " promise---test" << endl;
promise<int> myprom; //用于 线程间通信
thread t2(mythread2, ref(myprom), 10);
t2.detach();
//获取结果值
shared_future<int> ful = myprom.get_future(); //绑定
thread t3(mythread3, ref(ful)); //mythread3等待获取参数
thread t4(mythread4, ref(ful)); //mythread4等待获取参数
t3.join();
t4.join();
//auto result3 = ful.get(); //get只能调用一次
//cout << "result3=" << result3 << endl << endl;
}