当前位置: 首页 > news >正文

做网站框架可用jpg图吗/班级优化大师的功能

做网站框架可用jpg图吗,班级优化大师的功能,网站支付宝接口付费,东营专业网站建设公司电话C11中的std::async是个模板函数。std::async异步调用函数,在某个时候以Args作为参数(可变长参数)调用Fn,无需等待Fn执行完成就可返回,返回结果是个std::future对象。Fn返回的值可通过std::future对象的get成员函数获取。一旦完成Fn的执行&…

C++11中的std::async是个模板函数。std::async异步调用函数,在某个时候以Args作为参数(可变长参数)调用Fn,无需等待Fn执行完成就可返回,返回结果是个std::future对象。Fn返回的值可通过std::future对象的get成员函数获取。一旦完成Fn的执行,共享状态将包含Fn返回的值并ready。

std::async有两个版本:

1.无需显示指定启动策略,自动选择,因此启动策略是不确定的,可能是std::launch::async,也可能是std::launch::deferred,或者是两者的任意组合,取决于它们的系统和特定库实现。

2.允许调用者选择特定的启动策略。

std::async的启动策略类型是个枚举类enum class launch,包括:

  1. std::launch::async:异步,启动一个新的线程调用Fn,该函数由新线程异步调用,并且将其返回值与共享状态的访问点同步。

  2. std::launch::deferred:延迟,在访问共享状态时该函数才被调用。对Fn的调用将推迟到返回的std::future的共享状态被访问时(使用std::future的wait或get函数)。

参数Fn:可以为函数指针、成员指针、任何类型的可移动构造的函数对象(即类定义了operator()的对象)。Fn的返回值或异常存储在共享状态中以供异步的std::future对象检索。

参数Args:传递给Fn调用的参数,它们的类型应是可移动构造的。

返回值:当Fn执行结束时,共享状态的std::future对象准备就绪。std::future的成员函数get检索的值是Fn返回的值。当启动策略采用std::launch::async时,即使从不访问其共享状态,返回的std::future也会链接到被创建线程的末尾。在这种情况下,std::future的析构函数与Fn的返回同步。

std::future介绍参考:https://blog.csdn.net/WHEgqing/article/details/121182552

详细用法见下面的测试代码,下面是从其他文章中copy的测试代码,部分作了调整,详细内容介绍可以参考对应的reference:

#include "future.hpp"
#include <iostream>
#include <future>
#include <chrono>
#include <utility>
#include <thread>
#include <functional>
#include <memory>
#include <exception> 
#include <numeric>
#include <vector>
#include <cmath>
#include <string>
#include <mutex>namespace future_ {///
// reference: http://www.cplusplus.com/reference/future/async/
int test_async_1()
{auto is_prime = [](int x) {std::cout << "Calculating. Please, wait...\n";for (int i = 2; i < x; ++i) if (x%i == 0) return false;return true;};// call is_prime(313222313) asynchronously:std::future<bool> fut = std::async(is_prime, 313222313);std::cout << "Checking whether 313222313 is prime.\n";// ...bool ret = fut.get(); // waits for is_prime to returnif (ret) std::cout << "It is prime!\n";else std::cout << "It is not prime.\n";return 0;
}///
// reference: http://www.cplusplus.com/reference/future/launch/
int test_async_2()
{auto print_ten = [](char c, int ms) {for (int i = 0; i < 10; ++i) {std::this_thread::sleep_for(std::chrono::milliseconds(ms));std::cout << c;}};std::cout << "with launch::async:\n";std::future<void> foo = std::async(std::launch::async, print_ten, '*', 100);std::future<void> bar = std::async(std::launch::async, print_ten, '@', 200);// async "get" (wait for foo and bar to be ready):foo.get(); // 注:注释掉此句,也会输出'*'bar.get();std::cout << "\n\n";std::cout << "with launch::deferred:\n";foo = std::async(std::launch::deferred, print_ten, '*', 100);bar = std::async(std::launch::deferred, print_ten, '@', 200);// deferred "get" (perform the actual calls):foo.get(); // 注:注释掉此句,则不会输出'**********'bar.get();std::cout << '\n';return 0;
}///
// reference: https://en.cppreference.com/w/cpp/thread/async
std::mutex m;struct X {void foo(int i, const std::string& str) {std::lock_guard<std::mutex> lk(m);std::cout << str << ' ' << i << '\n';}void bar(const std::string& str) {std::lock_guard<std::mutex> lk(m);std::cout << str << '\n';}int operator()(int i) {std::lock_guard<std::mutex> lk(m);std::cout << i << '\n';return i + 10;}
};template <typename RandomIt>
int parallel_sum(RandomIt beg, RandomIt end)
{auto len = end - beg;if (len < 1000)return std::accumulate(beg, end, 0);RandomIt mid = beg + len / 2;auto handle = std::async(std::launch::async, parallel_sum<RandomIt>, mid, end);int sum = parallel_sum(beg, mid);return sum + handle.get();
}int test_async_3()
{std::vector<int> v(10000, 1);std::cout << "The sum is " << parallel_sum(v.begin(), v.end()) << '\n';X x;// Calls (&x)->foo(42, "Hello") with default policy:// may print "Hello 42" concurrently or defer executionauto a1 = std::async(&X::foo, &x, 42, "Hello");// Calls x.bar("world!") with deferred policy// prints "world!" when a2.get() or a2.wait() is calledauto a2 = std::async(std::launch::deferred, &X::bar, x, "world!");// Calls X()(43); with async policy// prints "43" concurrentlyauto a3 = std::async(std::launch::async, X(), 43);a2.wait();                     // prints "world!"std::cout << a3.get() << '\n'; // prints "53"return 0;
} // if a1 is not done at this point, destructor of a1 prints "Hello 42" here///
// reference: https://thispointer.com/c11-multithreading-part-9-stdasync-tutorial-example/
int test_async_4()
{using namespace std::chrono;auto fetchDataFromDB = [](std::string recvdData) {// Make sure that function takes 5 seconds to completestd::this_thread::sleep_for(seconds(5));//Do stuff like creating DB Connection and fetching Datareturn "DB_" + recvdData;};auto fetchDataFromFile = [](std::string recvdData) {// Make sure that function takes 5 seconds to completestd::this_thread::sleep_for(seconds(5));//Do stuff like fetching Data Filereturn "File_" + recvdData;};// Get Start Timesystem_clock::time_point start = system_clock::now();std::future<std::string> resultFromDB = std::async(std::launch::async, fetchDataFromDB, "Data");//Fetch Data from Filestd::string fileData = fetchDataFromFile("Data");//Fetch Data from DB// Will block till data is available in future<std::string> object.std::string dbData = resultFromDB.get();// Get End Timeauto end = system_clock::now();auto diff = duration_cast <std::chrono::seconds> (end - start).count();std::cout << "Total Time Taken = " << diff << " Seconds" << std::endl;//Combine The Datastd::string data = dbData + " :: " + fileData;//Printing the combined Datastd::cout << "Data = " << data << std::endl;return 0;
}} // namespace future_
http://www.jmfq.cn/news/5108941.html

相关文章:

  • 广东新闻联播直播回放/在线网站seo诊断
  • 电子烟网站建设/百度网址大全旧版本
  • 专门做海产品的网站/房地产销售工作内容
  • 关于营销的网站有哪些/宁德seo公司
  • 网站的软件维护包括什么/网页制作教程步骤
  • 网站建设包涵哪些领域/关键词快速排名不限行业
  • 沧州做英文网站哪家公司好/营销型网站建设怎么做
  • 杭州微网站建设公司哪家好/免费下载百度软件
  • 关于景区网站规划建设方案书/品牌营销和市场营销的区别
  • 珠海斗门建设局网站/海口seo快速排名优化
  • 无锡建设教育协会网站/台州关键词优化服务
  • 网站建设 定制/有没有购买链接
  • 给平顶山公安局做网站的公司/网站排名优化培训哪家好
  • 网站设计规划方案/可以免费推广的平台
  • 新浪博客怎么给自己网站做链接吗/宁波seo搜索引擎优化公司
  • 广州化妆品网站建设/公司网络营销策划书
  • 美仑美家具的网站谁做的/湖南株洲疫情最新情况
  • 新建网站做优化/win11优化大师
  • 厦门做网站/seo云优化
  • 滁州做网站hi444/毛戈平化妆培训学校官网
  • 西安搬家公司电话/刘连康seo培训哪家强
  • 给客户做网站 客户不付尾款/成都网站建设系统
  • 海报设计思路/seo网络推广外包公司
  • 快速知彼网络网站建设/南宁网站快速排名提升
  • 建设网站公司哪个好/电商网站搭建
  • 是在百度中建设网站/最新新闻
  • 事业单位网站建设方案/50个市场营销经典案例
  • 手机怎么做电子书下载网站/长春网站推广排名
  • wordpress图片上传压缩/seo快速软件
  • 淘宝做网站很便宜/app推广接单发布平台