C++下异步编程async使用

目的:使用async的目的是为了简化thread,并且可以不需要每次都创建线程。

作用:

       1、指定未来某个时间段给出结果或做某件事,中间过程并不关心

        2、async可以处理计算量非常大,非常复杂的查找

函数接口:

  template<typename _Fn, typename... _Args>
    future<typename result_of<_Fn(_Args...)>::type>
    async(launch __policy, _Fn&& __fn, _Args&&... __args)

 这个是一个函数模板,具体的参数如下:

launch __policy:函数执行的方式,默认两种方式
                  /// Launch code for futures
                  enum class launch
                  {
                    async = 1, //异步启动,在调用std::async()时创建一个新的线程以异步调用函            数,并返回future对象;
                    deferred = 2//延迟启动,在调用std::async()时不创建线程,直到调用了future对象的get()或wait()方法时,才创建线程;
                  };
_Fn&& __fn    :函数名称
_Args&&... __args:函数的参数

example:不比比直接上代码!


#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <future>
#include <unistd.h>
using namespace std;
 void  test(int t)
 {
	std::cout<<"1111"<<'\n';
 }
void fun1()
{
	sleep(1);
	std::cout << "fun1..." << std:: endl;
}
void fun2(int n)
{
	sleep(2);
	std::cout << "fun2 n:"<<n << std::endl;
}
std::string fun3(const std::string& str)
{
	cout << str << endl;
	return str;
}
int main(){
	std::async(std::launch::async,test,1);
	auto pfun1 = std::async(std::launch::async, fun1);
	auto pfun2 = std::async(std::launch::async, fun2,2);
	auto pfun3 = std::async(std::launch::async, fun3,"hello async");
	// lambda 表达式
	auto pfun4 = std::async(std::launch::async,[] {
		cout << "pfun4" << endl;
	});
	sleep(10);
}

结果如下:不用多说,直接拿来就用!

1111
hello async
pfun4
fun1...
fun2 n:2


                
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用C++语言实现多线程异步编程的代码示例: 多线程示例: ```cpp #include <iostream> #include <thread> // 线程函数,打印数字 void printNumbers() { for (int i = 1; i <= 10; ++i) { std::cout << i << std::endl; } } // 线程函数,打印字母 void printLetters() { for (char letter : {'a', 'b', 'c', 'd', 'e'}) { std::cout << letter << std::endl; } } int main() { // 创建两个线程 std::thread thread1(printNumbers); std::thread thread2(printLetters); // 等待两个线程执行完毕 thread1.join(); thread2.join(); std::cout << "Done!" << std::endl; return 0; } ``` 上述代码创建了两个线程,一个打印数字,一个打印字母。两个线程可以并行执行,提高了程序的性能。 异步编程示例: ```cpp #include <iostream> #include <string> #include <future> // 异步函数,获取网站HTML内容 std::string fetchWebsite(const std::string& url) { // 省略异步获取HTML内容的代码 return "HTML content"; } int main() { std::string url = "http://www.example.com"; // 异步获取网站HTML内容 std::future<std::string> futureHTML = std::async(std::launch::async, fetchWebsite, url); // 程序可以继续执行其他任务,不必等待HTML内容返回 std::cout << "Program continues..." << std::endl; // 等待HTML内容返回,并打印出来 std::string html = futureHTML.get(); std::cout << html << std::endl; return 0; } ``` 上述代码使用C++11标准的异步编程特性,异步获取一个网站的HTML内容,并打印出来。异步编程可以提高程序的响应速度和并发性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值