文章目录
1、std::async、std::future创建后台任务并返回值
1.1 std::async
std::async
是个函数模板,用来启动一个异步任务,启动起来一个异步任务之后,他返回一个std::future
对象,std::future
是个类模板。- 启动一个异步任务:就是创建一个线程并开始执行对应的线程入口函数,它返回一个
std::future
对象,这个std::future
对象里面就含有线程入口函数所返回的结果,其实就是线程返回的结果,可以通过调用std::future
对象的成员函数get()
来获取结果。
1.2 std::future
std::async
返回的对象,面就含有线程返回的结果。future
:将来的意思,有人也称呼std::future
提供了一种访问异步操作结果的机制,就是说这个结果你可能没有办法马上拿到,但在线程执行完毕的时候,你就能拿到结果了,所以可以理解成:这个future
里会保存一个值。
1.3 std::future::get()
std::future
对象使用get()
函数等待线程执行结束并返回结果,get()
函数会一直卡在那里等待拿值,不拿到返回值,誓不罢休。get()
函数只能调用一次,不能调用多次,如果第二次get
一个future
对象会得到一个异常,主要是因为get
函数的设计是一个移动语义,底层调用了std::move()
。
1.4 std::future::wait()
- 如果
std::future
对象使用wait()
函数,则为等待线程返回,本身并不返回结果。 wait()
函数可以调用多次,但除第一次调用wait()
有效外,其余次均无效。
1.5 使用普通函数创建异步任务
示例代码:
#include <iostream>
#include <thread>
#include <mutex>
#include <future>
using namespace std;
//线程入口函数
int mythread(int mypar)
{
cout << mypar << endl;
//打印新线程id值
cout << "mythread() start " << "threadid = " << std::this_thread::get_id() << endl;
std::chrono::milliseconds dura(5000); //休息5s
std::this_thread::sleep_for(dura);
cout << "mythread() end " << "threadid = " << std::this_thread::get_id() << endl;
return 5;
}
int main()
{
cout << "main " << "threadid = " << std::this_thread::get_id() << endl;
//创建一个线程并开始执行,将future对象和async创建的线程绑定到了一起,流程不卡到这里
std::future<int> result = std::async(mythread);
cout << "continue......!" << endl;
int def;
def = 0;
//程序通过std::future对象的get()成员函数等待线程执行结束并返回结果
//这个get()函数很牛,不拿到将来的返回值,誓不罢休,不拿导致就卡在这里等待拿值
cout << result.get() << endl;
//result.wait();
cout << "I love China!" << endl;
return 0;
}
1.6 使用类成员函数创建异步任务
示例代码:
#include <iostream>
#include <thread>
#include <mutex>
#include <future>
using namespace std;
class A
{
public:
//线程入口函数
int mythread(int mypar)
{
cout << mypar << endl;
//打印新线程id值
cout << "mythread() start " << "threadid = " << std::this_thread::get_id() << endl;
std::chrono::milliseconds dura(5000); //休息5s
std::this_thread::sleep_for(dura);
cout << "mythread() end " << "threadid = " << std::this_thread::get_id() << endl;
return 5;
}
};
int main()
{
A a;
int tmppar = 12;
cout << "main " << "threadid = " << std::this_thread::get_id() << endl;
//创建一个线程并开始执行,将future对象和async创建的线程绑定到了一起,流程不卡到这里
//第二个参数为对象引用,才能保证线程里用的是同一个对象
std::future<int> result = std::async(&A::mythread, &a, tmppar);
cout << "continue......!" << endl;
int def;
def = 0;
cout << result.get() << endl;
//result.wait();
cout << "I love China!" << endl;
return 0;
}
1.7 std::lanuch
- 我们通过额外向
std::async()
传递一个参数,该参数类型是std::lanuch
类型(枚举类型),来达到一些特殊的目的。
1.7.1 std::lanuch::deferred
-
std::lanuch::deferred
:表示线程入口函数调用被延迟到std::future
的wait()
或者get()
函数调用时才执行;如果wait()
或者get()
没有被调用,那么线程就不会被执行,实际上,线程根本就没创建。#include <iostream> #include <thread> #include <mutex> #include <future> using namespace std; class A { public: //线程入口函数 int mythread(int mypar) { cout << mypar << endl; //打印新线程id值 cout << "mythread() start " << "threadid = " << std::this_thread::get_id() << endl; std::chrono::milliseconds dura(5000); //休息5s std::this_thread::sleep_for(dura); cout << "mythread() end " << "threadid = " << std::this_thread::get_id() << endl; return 5; } }; int main() { A a; int tmppar = 12; cout << "main " << "threadid = " << std::this_thread::get_id() << endl; //创建一个线程并开始执行,将future对象和async创建的线程绑定到了一起,流程不卡到这里 //第二个参数为对象引用,才能保证线程里用的是同一个对象 std::future<int> result = std::async(std::launch::deferred,&A::mythread, &a, tmppar); //std::future<int> result = std::async(std::launch::async,&A::mythread, &a, tmppar); cout << "continue......!" << endl; int def; def = 0; cout << result.get() << endl; //result.wait(); cout << "I love China!" << endl; return 0; }
当
get()
函数没有被调用,代码执行结果如图:结果说明:如果
wait()
或者get()
没有被调用,那么线程就不会被执行,实际上,线程根本就没创建。 -
当调用了
get()
函数,代码执行结果如图:可以看到,子线程ID和主线程ID是一样的,说明当对
async()
使用std::launch::deferred
参数时,根本没有创建子线程,自始至终都只有一个主线程,是在主线程中调用的线程入口函数。
1.7.2 std::lanuch::async
std::lanuch::async
:表示在调用std::async()
函数的时候就开始创建线程,强制创建一个线程。
2、std::packaged_task
std::packaged_task
:是个类模板,它的模板参数是各种可调用对象;- 通过
std::packaged_task
把各种可调用对象包装起来,方便将来作为线程入口函数来调用。
std::packaged_task
包装普通调用函数示例代码:
#include <iostream>
#include <thread>
#include <vector>
#include <list>
#include <mutex>
#include <future>
using namespace std;
//线程入口函数
int mythread(int mypar)
{
cout << mypar << endl;
//打印新线程id值
cout << "mythread() start " << "threadid = " << std::this_thread::get_id() << endl;
std::chrono::milliseconds dura(5000); //休息5s
std::this_thread::sleep_for(dura);
cout << "mythread() end " << "threadid = " << std::this_thread::get_id() << endl;
return 5;
}
int main()
{
cout << "main " << "threadid = " << std::this_thread::get_id() << endl;
std::packaged_task<int(int)> mypt(mythread); //我们把函数mythread通过packaged_task包装起来
std::thread t1(std::ref(mypt), 1); //线程直接开始执行,第二个参数为线程入口函数的参数
t1.join(); //等待线程执行完毕
//std::future对象里包含有线程入口函数的返回结果,这里result保存mythread函数返回的结果
std::future<int> result = mypt.get_future();
cout << result.get() << endl;
cout << "I love China!" << endl;
return 0;
}
std::packaged_task
包装lambda
表达式示例代码:
#include <iostream>
#include <thread>
#include <vector>
#include <list>
#include <mutex>
#include <future>
using namespace std;
int main()
{
cout << "main " << "threadid = " << std::this_thread::get_id() << endl;
std::packaged_task<int(int)> mypt([](int mypar)
{
cout << mypar << endl;
//打印新线程id值
cout << "mythread() start " << "threadid = " << std::this_thread::get_id() << endl;
std::chrono::milliseconds dura(5000); //休息5s
std::this_thread::sleep_for(dura);
cout << "mythread() end " << "threadid = " << std::this_thread::get_id() << endl;
return 5;
});
std::thread t1(std::ref(mypt), 1); //线程直接开始执行,第二个参数为线程入口函数的参数
t1.join(); //等待线程执行完毕
//std::future对象里包含有线程入口函数的返回结果,这里result保存mythread函数返回的结果
std::future<int> result = mypt.get_future();
cout << result.get() << endl;
cout << "I love China!" << endl;
return 0;
}
结果如下,子线程和主线程id
不同:
-
std::packaged_task
包装起来的可调用对象还可以直接调用,从这个角度来讲,std::packaged_task
对象也是一个可调用对象。 -
如果直接调用
std::packaged_task
对象,相当于执行一个函数,就不会创建一个新的线程。示例代码:
#include <iostream> #include <thread> #include <vector> #include <list> #include <mutex> #include <future> using namespace std; int main() { cout << "main " << "threadid = " << std::this_thread::get_id() << endl; std::packaged_task<int(int)> mypt([](int mypar) { cout << mypar << endl; //打印新线程id值 cout << "mythread() start " << "threadid = " << std::this_thread::get_id() << endl; std::chrono::milliseconds dura(5000); //休息5s std::this_thread::sleep_for(dura); cout << "mythread() end " << "threadid = " << std::this_thread::get_id() << endl; return 5; }); mypt(105); //直接调用 std::future<int> result = mypt.get_future(); cout << result.get() << endl; cout << "I love China!" << endl; return 0; }
结果如下:
-
使用容器对
std::packaged_task
对象进行存储并调用,要使用std::move()
来移动对象,不然会消耗资源进行复制。代码如下:
#include <iostream> #include <thread> #include <vector> #include <list> #include <mutex> #include <future> using namespace std; vector<std::packaged_task<int(int)>> mytasks; //容器 int main() { cout << "main " << "threadid = " << std::this_thread::get_id() << endl; std::packaged_task<int(int)> mypt([](int mypar) { cout << mypar << endl; //打印新线程id值 cout << "mythread() start " << "threadid = " << std::this_thread::get_id() << endl; std::chrono::milliseconds dura(5000); //休息5s std::this_thread::sleep_for(dura); cout << "mythread() end " << "threadid = " << std::this_thread::get_id() << endl; return 5; }); mytasks.push_back(std::move(mypt)); //入容器,这里使用了移动语义,入进去之后mypt就为空 std::packaged_task<int(int)> mypt2; auto iter = mytasks.begin(); mypt2 = std::move(*iter); //使用移动语义 mytasks.erase(iter); //删除第一个元素,迭代已经失效了,所以后续代码不可以再使用iter mypt2(123); std::future<int> result = mypt2.get_future(); cout << result.get() << endl; cout << "I love China!" << endl; return 0; }
结果如下,同样不会创建新线程:
3、std::promise
std::promise
:类模板。- 作用:能够在某个线程中给它赋值,然后可以在其他线程中,把这个值取出来用。
示例代码:
#include <iostream>
#include <thread>
#include <vector>
#include <list>
#include <mutex>
#include <future>
using namespace std;
void mythread(std::promise<int>& tmpp, int calc) //注意第一个参数写法
{
cout << "mythread()id = " << std::this_thread::get_id() << endl;
//这里做一系列复杂操作
calc++;
calc *= 10;
//做其他运算,比如整整花费了5秒钟
std::chrono::milliseconds dura(5000);
std::this_thread::sleep_for(dura);
//终于计算出结果了
int result = calc; //保存结果
tmpp.set_value(result); //结果保存到了tmpp这个对象中
return;
}
int main()
{
cout << "main " << "threadid = " << std::this_thread::get_id() << endl;
std::promise<int> myprom; //声明一个std::promise对象myprom,保存值类型为int
std::thread t1(mythread, std::ref(myprom), 180);
t1.join();
//获取结果值
std::future<int> fu1 = myprom.get_future(); //promise和future绑定,用于获取线程返回值
auto result = fu1.get(); //get只能调用一次,不能调用多次
cout << "result = " << result << endl;
cout << "I love China!" << endl;
return 0;
}
结果如下:
-
将一个线程的结果传递到另一个线程使用
示例代码:
#include <iostream> #include <thread> #include <vector> #include <list> #include <mutex> #include <future> using namespace std; void mythread(std::promise<int>& tmpp, int calc) //注意第一个参数写法 { cout << "mythread()id = " << std::this_thread::get_id() << endl; //这里做一系列复杂操作 calc++; calc *= 10; //做其他运算,比如整整花费了5秒钟 std::chrono::milliseconds dura(5000); std::this_thread::sleep_for(dura); //终于计算出结果了 int result = calc; //保存结果 tmpp.set_value(result); //结果保存到了tmpp这个对象中 return; } void mythread2(std::future<int>& tmpf) { cout << "mythread2()id = " << std::this_thread::get_id() << endl; auto result = tmpf.get(); cout << "mythread2 result = " << result << endl; return; } int main() { cout << "main " << "threadid = " << std::this_thread::get_id() << endl; std::promise<int> myprom; //声明一个std::promise对象myprom,保存值类型为int std::thread t1(mythread, std::ref(myprom), 180); t1.join(); //获取结果值 std::future<int> fu1 = myprom.get_future(); //promise和future绑定,用于获取线程返回值 std::thread t2(mythread2, std::ref(fu1)); t2.join(); //等mythread2执行完毕 cout << "I love China!" << endl; return 0; }
结果如下:
4、小结
- 我们学习这些东西的目的,并不是要把他们都用在咱们自己的实际开发中。
- 相反,如果我们能够用最少的东西能够写出一个稳定、高效的多线程程序,更值得赞赏。
- 我们为了成长,必须要阅读一些高手写的代码,从而快速实现自己代码的积累,我们的技术就会有一个大幅度的提升。
注:本人学习c++多线程视频地址:C++多线程学习地址