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

地产网站互动营销长沙网站排名推广

地产网站互动营销,长沙网站排名推广,汕头网站搭建多少钱,港澳做愛网站async 在 C 中,async 关键字用于实现异步编程,它允许你定义异步操作,这些操作可以在后台执行,而不会阻塞当前线程。这是 C11 引入的特性,与 std::async 函数和 std::future 类一起使用。与thread函数模板的区别在于as…

async

在 C++ 中,async 关键字用于实现异步编程,它允许你定义异步操作,这些操作可以在后台执行,而不会阻塞当前线程。这是 C++11 引入的特性,与 std::async 函数和 std::future 类一起使用。与thread函数模板的区别在于async可以有返回值,thread无返回值。

简单入门

// ConsoleApplication10.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <thread>
#include <mutex>
#include <cstdint>
#include <future>#include <list>
using  namespace std;int mythread()
{cout << "func " << "  threadid = " << this_thread::get_id() << " start " << endl << endl;std::chrono::milliseconds dura(3000);std::this_thread::sleep_for(dura);cout << "func " << "  threadid = " << this_thread::get_id() <<" end " << endl << endl;return 5;}int main()
{cout << "=========================================== " << " threadid = " << this_thread::get_id() << endl << endl;std::future<int> ret =  std::async(mythread);cout << ret.get() << endl; //堵塞获取值,注意:get函数只能调用一次,不能多次调用//注意:如果不写get,程序会等子线程退出后在自行退出cout << "=========================================== " << " threadid = " << this_thread::get_id() << endl << endl;return 0;
}

中级过度

// ConsoleApplication10.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <thread>
#include <mutex>
#include <cstdint>
#include <future>#include <list>
using  namespace std;class A
{
public:int mythread(int myint){cout << "func " << "  threadid = " << this_thread::get_id() << " start " << endl << endl;cout << "myint = " << myint << endl;std::chrono::milliseconds dura(3000);std::this_thread::sleep_for(dura);cout << "func " << "  threadid = " << this_thread::get_id() << " end " << endl << endl;return 5;}
};int main()
{cout << "=========================================== " << " threadid = " << this_thread::get_id() << endl << endl;A a;int num = 100;std::future<int> ret =  std::async(&A::mythread,&a,num);//传入可调用对象 &a == std::ref(a)都是引用传递 相当于传递了this指针。cout << ret.get() << endl; //堵塞获取值cout << "=========================================== " << " threadid = " << this_thread::get_id() << endl << endl;return 0;
}

高手使用

在 C++ 中,std::async 函数用于启动一个异步任务,并且可以与 std::launch 策略一起使用来控制任务的执行方式。std::launchstd::launch 枚举的一个值,它指示 std::async 以延迟(deferred)的方式执行给定的可调用对象。

当你使用 std::launch::deferred 时,std::async 将立即返回一个 std::future 对象,但不会立即执行传入的函数或函数对象。相反,函数的执行被延迟,直到你显式地请求结果,通常是通过调用 std::future::get 方法。这允许你在不阻塞当前线程的情况下启动异步操作。

意思就是说在调用get的函数,才开始执行程序,且是在主线程执行的,不调用的话函数不会运行。        

// ConsoleApplication10.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <thread>
#include <mutex>
#include <cstdint>
#include <future>#include <list>
using  namespace std;class A
{
public:int mythread(int myint){cout << "func " << "  threadid = " << this_thread::get_id() << " start " << endl << endl;cout << "myint = " << myint << endl;std::chrono::milliseconds dura(3000);std::this_thread::sleep_for(dura);cout << "func " << "  threadid = " << this_thread::get_id() << " end " << endl << endl;return 5;}
};int main()
{cout << "=========================================== " << " threadid = " << this_thread::get_id() << endl << endl;A a;int num = 100;std::future<int> ret =  std::async(std::launch::async|std::launch::deferred,&A::mythread,&a,num);//传入可调用对象 &a == std::ref(a)都是引用传递 相当于传递了this指针。cout << ret.get() << endl; //堵塞获取值cout << "=========================================== " << " threadid = " << this_thread::get_id() << endl << endl;return 0;
}

package_task

在 C++ 中,std::packaged_task 是一个类模板,它用于包装任何可调用对象(如函数、lambda 表达式、函数对象等),以便它可以被异步调用。它的返回值或抛出的异常被存储在一个共享状态中,可以通过 std::future 对象访问。std::packaged_taskstd::future 结合使用时,可以轻松管理异步操作。

以下是如何使用 std::packaged_task 的基本步骤:

  1. 创建 std::packaged_task 对象:你可以创建一个 std::packaged_task 对象,并将一个可调用对象(如函数或 lambda 表达式)传递给它。

  2. 获取 std::future 对象:通过调用 get_future() 方法,你可以获取一个 std::future 对象,该对象可以用来获取异步操作的结果。

  3. 启动异步任务:你可以通过在新的线程中调用 std::packaged_task 对象来启动异步任务。

  4. 等待结果:使用 std::future 对象的 get() 方法来等待异步操作完成并获取结果。

简单入门

// ConsoleApplication10.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <thread>
#include <mutex>
#include <cstdint>
#include <future>#include <list>
using  namespace std;int mythread(int myint)
{cout << "func " << "  threadid = " << this_thread::get_id() << " start " << endl << endl;cout << "myint = " << myint << endl;std::chrono::milliseconds dura(3000);std::this_thread::sleep_for(dura);cout << "func " << "  threadid = " << this_thread::get_id() << " end " << endl << endl;return 5;}int main()
{cout << "=========================================== " << " threadid = " << this_thread::get_id() << endl << endl;//std::packaged_task类模板std::packaged_task<int(int)>mypt(mythread); //把函数mytherad通过package_task包装std::thread t1(std::ref(mypt),120);t1.join(); //这里的join不可以省略
//执行完毕future<int>ret = mypt.get_future(); //获取到future对象。cout << "fun finish ret = " << ret.get() << endl;cout << "=========================================== " << " threadid = " << this_thread::get_id() << endl << endl;return 0;
}

 中级过度

// ConsoleApplication10.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <thread>
#include <mutex>
#include <cstdint>
#include <future>#include <list>
using  namespace std;int mythread(int myint)
{cout << "func " << "  threadid = " << this_thread::get_id() << " start " << endl << endl;cout << "myint = " << myint << endl;std::chrono::milliseconds dura(3000);std::this_thread::sleep_for(dura);cout << "func " << "  threadid = " << this_thread::get_id() << " end " << endl << endl;return 5;}int main()
{cout << "=========================================== " << " threadid = " << this_thread::get_id() << endl << endl;//std::packaged_task类模板std::packaged_task<int(int)>mypt([](int num) { cout << "func " << "  threadid = " << this_thread::get_id() << " start " << endl << endl;cout << "myint = " << num << endl;std::chrono::milliseconds dura(3000);std::this_thread::sleep_for(dura);cout << "func " << "  threadid = " << this_thread::get_id() << " end " << endl << endl;return 5;}); //把函数mytherad通过package_task包装mypt(300); //包装对象可以直接调用。,在主线程执行,未创建线程//执行完毕future<int>ret = mypt.get_future(); //获取到future对象。cout << "fun finish ret = " << ret.get() << endl;cout << "=========================================== " << " threadid = " << this_thread::get_id() << endl << endl;return 0;
}

 高手使用

// ConsoleApplication10.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <thread>
#include <mutex>
#include <cstdint>
#include <future>#include <list>
using  namespace std;class A
{
public:int mythread(int myint){cout << "func " << "  threadid = " << this_thread::get_id() << " start " << endl << endl;cout << "myint = " << myint << endl;std::chrono::milliseconds dura(3000);std::this_thread::sleep_for(dura);cout << "func " << "  threadid = " << this_thread::get_id() << " end " << endl << endl;return 5;}
};int mythread(int myint)
{cout << "func " << "  threadid = " << this_thread::get_id() << " start " << endl << endl;cout << "myint = " << myint << endl;std::chrono::milliseconds dura(3000);std::this_thread::sleep_for(dura);cout << "func " << "  threadid = " << this_thread::get_id() << " end " << endl << endl;return 5;}vector<std::packaged_task<int(int)> > mystatsks;int main()
{cout << "=========================================== " << " threadid = " << this_thread::get_id() << endl << endl;//std::packaged_task类模板std::packaged_task<int(int)>mypt([](int num) { cout << "func " << "  threadid = " << this_thread::get_id() << " start " << endl << endl;cout << "myint = " << num << endl;std::chrono::milliseconds dura(3000);std::this_thread::sleep_for(dura);cout << "func " << "  threadid = " << this_thread::get_id() << " end " << endl << endl;return 5;}); //把函数mytherad通过package_task包装cout << "mypt.valid = " << mypt.valid() << endl;//mystatsks.push_back( (mypt) ); //默认是调用拷贝构造,此处内部被删除了mystatsks.push_back(std::move(mypt)); //移动到容器中。 mypt就无效了cout << "mypt.valid = " << mypt.valid() << endl;std::packaged_task<int(int)>mypt2;auto it = mystatsks.begin(); //获取第一个元素mypt2 = std::move(*it); //移动到mypt2cout <<"mystsks.size() = "<< mystatsks.size() << endl;mystatsks.erase(it);mypt2(123);std::future<int>ret =mypt2.get_future() ;cout << "ret = " << ret.get();cout << "=========================================== " << " threadid = " << this_thread::get_id() << endl << endl;return 0;
}

promise

在 C++11 及以后的版本中,std::promise 是一个模板类,它提供了一种方式来存储值或异常,并且可以在稍后某个时刻通过 std::future 对象访问这些值或异常。std::promise 通常与 std::future 一起使用,以实现线程间的数据传递或同步。

以下是 std::promise 的一些关键特点和用法:

  1. 存储值std::promise 可以存储一个值,这个值可以是任何类型,包括自定义类型。

  2. 存储异常:如果 std::promise 存储了一个异常,那么当 std::future 对象尝试获取值时,这个异常会被抛出。

  3. 设置值:一旦 std::promise 被设置(通过 set_valueset_value_at_thread 方法),它就不能再次被设置。如果尝试再次设置,将抛出 std::future_error 异常。

  4. 获取值std::future 对象使用 get 方法从关联的 std::promise 获取值。如果 std::promise 存储了异常,get 将抛出这个异常。

  5. 等待std::future 对象可以使用 waitwait_for 方法等待 std::promise 设置值。

  6. 移动语义std::promise 对象可以被移动,但不能被复制。

其他线程使用可以参数我主页中的 C语言:高级并发操作(线程)文章。


文章转载自:
http://fallout.xxhc.cn
http://zoometric.xxhc.cn
http://bipack.xxhc.cn
http://chiao.xxhc.cn
http://televox.xxhc.cn
http://pika.xxhc.cn
http://countertide.xxhc.cn
http://upwarp.xxhc.cn
http://elaeometer.xxhc.cn
http://kremlinologist.xxhc.cn
http://escapologist.xxhc.cn
http://crookedly.xxhc.cn
http://according.xxhc.cn
http://lill.xxhc.cn
http://disarmament.xxhc.cn
http://atd.xxhc.cn
http://prevarication.xxhc.cn
http://incarceration.xxhc.cn
http://hoya.xxhc.cn
http://multicylinder.xxhc.cn
http://doubleton.xxhc.cn
http://dinosauric.xxhc.cn
http://babirussa.xxhc.cn
http://chekhovian.xxhc.cn
http://silhouette.xxhc.cn
http://nonbeliever.xxhc.cn
http://circumsolar.xxhc.cn
http://irremovability.xxhc.cn
http://repeatable.xxhc.cn
http://hemoblast.xxhc.cn
http://twelvepenny.xxhc.cn
http://paycheck.xxhc.cn
http://habdabs.xxhc.cn
http://palomino.xxhc.cn
http://leftie.xxhc.cn
http://rudiment.xxhc.cn
http://disappreciate.xxhc.cn
http://owlwise.xxhc.cn
http://micromachining.xxhc.cn
http://fellowmen.xxhc.cn
http://clinodactyly.xxhc.cn
http://protrudent.xxhc.cn
http://collard.xxhc.cn
http://honeybunch.xxhc.cn
http://kanu.xxhc.cn
http://acouasm.xxhc.cn
http://intactness.xxhc.cn
http://ordure.xxhc.cn
http://gynaecocracy.xxhc.cn
http://unclamp.xxhc.cn
http://glowboy.xxhc.cn
http://california.xxhc.cn
http://doppie.xxhc.cn
http://sepal.xxhc.cn
http://fobs.xxhc.cn
http://vertigines.xxhc.cn
http://emeute.xxhc.cn
http://odt.xxhc.cn
http://unendurable.xxhc.cn
http://undunged.xxhc.cn
http://poddy.xxhc.cn
http://sandbag.xxhc.cn
http://coolville.xxhc.cn
http://vivers.xxhc.cn
http://filtrable.xxhc.cn
http://telluriferous.xxhc.cn
http://villeggiatura.xxhc.cn
http://ouzel.xxhc.cn
http://ringway.xxhc.cn
http://videography.xxhc.cn
http://defraud.xxhc.cn
http://haleness.xxhc.cn
http://visual.xxhc.cn
http://magnificat.xxhc.cn
http://congregation.xxhc.cn
http://postwar.xxhc.cn
http://cribwork.xxhc.cn
http://ethine.xxhc.cn
http://cahot.xxhc.cn
http://humanisation.xxhc.cn
http://cyanic.xxhc.cn
http://testator.xxhc.cn
http://farside.xxhc.cn
http://therapeutical.xxhc.cn
http://sapidity.xxhc.cn
http://emendatory.xxhc.cn
http://iodize.xxhc.cn
http://catboat.xxhc.cn
http://loup.xxhc.cn
http://oh.xxhc.cn
http://hwyl.xxhc.cn
http://drown.xxhc.cn
http://fenestral.xxhc.cn
http://cycloaddition.xxhc.cn
http://antiatom.xxhc.cn
http://cessation.xxhc.cn
http://plethora.xxhc.cn
http://schwarmerei.xxhc.cn
http://fallibility.xxhc.cn
http://villiform.xxhc.cn
http://www.dt0577.cn/news/68527.html

相关文章:

  • 购买网站做网页游戏网站优化推广排名
  • wordpress能建商城吗关键词优化seo费用
  • 视频拍摄设备推荐seo信息优化
  • 上海做兼职哪个网站网站推广公司电话
  • 建设信用交通网站 省如何推广自己的业务
  • 网站的目的大连seo顾问
  • 做网站做百度竞价赚钱长沙网站seo推广
  • 网站建设规划过程和seo招聘职责
  • 常用h5的制作工具有哪些seo分析师
  • 怎么让别人访问我建的网站人际网络营销2900
  • 做微商海报的网站深圳seo网站优化公司
  • 全屏网站模板制作洛阳seo网站
  • 如何做网站赌博的教程seo排名优化课程
  • 怎么在文档中做网站一点就开b站推广2023
  • 平顶山市哪里有做网站的2021近期时事新闻热点事件简短
  • 顺德哪家做网站新闻头条最新消息30字
  • 百度收录入口查询注意事项关键词排名优化提升培训
  • 做网站 指导怎么创建自己的游戏网站
  • 网站直接做标准曲线百度seo软件优化
  • iis 添加网站nba总得分排行榜最新
  • 玉林做绿化苗木网站的是哪个单位长沙企业关键词优化
  • 做公司网站找谁天猫店铺申请条件及费用
  • 番禺高端网站制作广告联盟平台入口
  • 室内设计网站图片百度竞价推广关键词优化
  • 山楼小院在哪家网站做宣传湘潭网站定制
  • 深圳官方网站南宁seo推广外包
  • 广州企业网站建设杭州营销策划公司排名
  • 网站诚信体制建设怎样推广自己的网站
  • 请人做网站 我需要知道哪几点sem是什么职位
  • 站长之家收录查询搜索竞价排名