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

个人网站建设模板如何做微信小程序?

个人网站建设模板,如何做微信小程序?,友情链接网站大全,网页设计与制作基础C11提供了一个原子类型std::atomicT#xff0c;通过这个原子类型管理的内部变量就可以称之为原子变量#xff0c;我们可以给原子类型指定bool、char、int、long、指针等类型作为模板参数#xff08;不支持浮点类型和复合类型#xff09;。 原子指的是一系列不可被…C11提供了一个原子类型std::atomicT通过这个原子类型管理的内部变量就可以称之为原子变量我们可以给原子类型指定bool、char、int、long、指针等类型作为模板参数不支持浮点类型和复合类型。 原子指的是一系列不可被CPU上下文交换的机器指令这些指令组合在一起就形成了原子操作。在多核CPU下当某个CPU核心开始运行原子操作时会先暂停其它CPU内核对内存的操作以保证原子操作不会被其它CPU内核所干扰。 由于原子操作是通过指令提供的支持因此它的性能相比锁和消息传递会好很多。相比较于锁而言原子类型不需要开发者处理加锁和释放锁的问题同时支持修改读取等操作还具备较高的并发性能几乎所有的语言都支持原子类型。 可以看出原子类型是无锁类型但是无锁不代表无需等待因为原子类型内部使用了CAS循环当大量的冲突发生时该等待还是得等待但是总归比锁要好。 C11内置了整形的原子变量这样就可以更方便的使用原子变量了。在多线程操作中使用原子变量之后就不需要再使用互斥量来保护该变量了用起来更简洁。因为对原子变量进行的操作只能是一个原子操作atomic operation原子操作指的是不会被线程调度机制打断的操作这种操作一旦开始就一直运行到结束中间不会有任何的上下文切换。多线程同时访问共享资源造成数据混乱的原因就是因为CPU的上下文切换导致的使用原子变量解决了这个问题因此互斥锁的使用也就不再需要了。 CAS全称是Compare and swap, 它通过一条指令读取指定的内存地址然后判断其中的值是否等于给定的前置值如果相等则将其修改为新的值。 1. atomic 类成员 1.1 类定义 // 定义于头文件 atomic template class T struct atomic; 通过定义可得知在使用这个模板类的时候一定要指定模板类型。  1.2 构造函数 // ① atomic() noexcept default; // ② constexpr atomic( T desired ) noexcept; // ③ atomic( const atomic ) delete;构造函数①默认无参构造函数。构造函数②使用 desired 初始化原子变量的值。构造函数③使用delete显示删除拷贝构造函数, 不允许进行对象之间的拷贝 。 1.3 公共成员函数  原子类型在类内部重载了  操作符并且不允许在类的外部使用  进行对象的拷贝。 T operator( T desired ) noexcept; T operator( T desired ) volatile noexcept;atomic operator( const atomic ) delete; atomic operator( const atomic ) volatile delete;原子地以 desired 替换当前值。按照 order 的值影响内存。  void store( T desired, std::memory_order order std::memory_order_seq_cst ) noexcept; void store( T desired, std::memory_order order std::memory_order_seq_cst ) volatile noexcept;1. desired存储到原子变量中的值。2. order强制的内存顺序 。 原子地加载并返回原子变量的当前值。按照 order 的值影响内存。直接访问原子对象也可以得到原子变量的当前值。 T load( std::memory_order order std::memory_order_seq_cst ) const noexcept; T load( std::memory_order order std::memory_order_seq_cst ) const volatile noexcept;1.4 类型别名  atomic_bool(C11)    std::atomicbool atomic_char(C11)    std::atomicchar atomic_schar(C11)    std::atomicsigned char atomic_uchar(C11)    std::atomicunsigned char atomic_short(C11)    std::atomicshort atomic_ushort(C11)    std::atomicunsigned short atomic_int(C11)    std::atomicint atomic_uint(C11)    std::atomicunsigned int atomic_long(C11)    std::atomiclong atomic_ulong(C11)    std::atomicunsigned long atomic_llong(C11)    std::atomiclong long atomic_ullong(C11)    std::atomicunsigned long long atomic_char8_t(C20)    std::atomicchar8_t atomic_char16_t(C11)    std::atomicchar16_t atomic_char32_t(C11)    std::atomicchar32_t atomic_wchar_t(C11)    std::atomicwchar_t atomic_int8_t(C11)(可选)    std::atomicstd::int8_t atomic_uint8_t(C11)(可选)    std::atomicstd::uint8_t atomic_int16_t(C11)(可选)    std::atomicstd::int16_t atomic_uint16_t(C11)(可选)    std::atomicstd::uint16_t atomic_int32_t(C11)(可选)    std::atomicstd::int32_t atomic_uint32_t(C11)(可选)    std::atomicstd::uint32_t atomic_int64_t(C11)(可选)    std::atomicstd::int64_t atomic_uint64_t(C11)(可选)    std::atomicstd::uint64_t atomic_int_least8_t(C11)    std::atomicstd::int_least8_t atomic_uint_least8_t(C11)    std::atomicstd::uint_least8_t atomic_int_least16_t(C11)    std::atomicstd::int_least16_t atomic_uint_least16_t(C11)    std::atomicstd::uint_least16_t atomic_int_least32_t(C11)    std::atomicstd::int_least32_t atomic_uint_least32_t(C11)    std::atomicstd::uint_least32_t atomic_int_least64_t(C11)    std::atomicstd::int_least64_t atomic_uint_least64_t(C11)    std::atomicstd::uint_least64_t atomic_int_fast8_t(C11)    std::atomicstd::int_fast8_t atomic_uint_fast8_t(C11)    std::atomicstd::uint_fast8_t atomic_int_fast16_t(C11)    std::atomicstd::int_fast16_t atomic_uint_fast16_t(C11)    std::atomicstd::uint_fast16_t atomic_int_fast32_t(C11)    std::atomicstd::int_fast32_t atomic_uint_fast32_t(C11)    std::atomicstd::uint_fast32_t atomic_int_fast64_t(C11)    std::atomicstd::int_fast64_t atomic_uint_fast64_t(C11)    std::atomicstd::uint_fast64_t atomic_intptr_t(C11)(可选)    std::atomicstd::intptr_t atomic_uintptr_t(C11)(可选)    std::atomicstd::uintptr_t atomic_size_t(C11)    std::atomicstd::size_t atomic_ptrdiff_t(C11)    std::atomicstd::ptrdiff_t atomic_intmax_t(C11)    std::atomicstd::intmax_t atomic_uintmax_t(C11)    std::atomicstd::uintmax_t 2. 原子变量的使用 2.1 互斥锁版本 #include iostream #include thread #include mutex #include atomic #include functional using namespace std;struct Counter {void increment(){for (int i 0; i 10; i){lock_guardmutex locker(m_mutex);m_value;cout increment number: m_value , theadID: this_thread::get_id() endl;this_thread::sleep_for(chrono::milliseconds(100));}}void decrement(){for (int i 0; i 10; i){lock_guardmutex locker(m_mutex);m_value--;cout decrement number: m_value , theadID: this_thread::get_id() endl;this_thread::sleep_for(chrono::milliseconds(100));}}int m_value 0;mutex m_mutex; };int main() {Counter c;auto increment bind(Counter::increment, c);auto decrement bind(Counter::decrement, c);thread t1(increment);thread t2(decrement);t1.join();t2.join();return 0; } 示例程序的执行结果为(当然执行的结果不唯一) decrement number: -1, theadID: 41376 decrement number: -2, theadID: 41376 decrement number: -3, theadID: 41376 decrement number: -4, theadID: 41376 decrement number: -5, theadID: 41376 increment number: -4, theadID: 25900 increment number: -3, theadID: 25900 increment number: -2, theadID: 25900 increment number: -1, theadID: 25900 increment number: 0, theadID: 25900 increment number: 1, theadID: 25900 increment number: 2, theadID: 25900 increment number: 3, theadID: 25900 increment number: 4, theadID: 25900 increment number: 5, theadID: 25900 decrement number: 4, theadID: 41376 decrement number: 3, theadID: 41376 decrement number: 2, theadID: 41376 decrement number: 1, theadID: 41376 decrement number: 0, theadID: 41376 2.2 原子变量版本:  #include iostream #include thread #include atomic #include functional using namespace std;struct Counter {void increment(){for (int i 0; i 10; i){m_value;cout increment number: m_value , theadID: this_thread::get_id() endl;this_thread::sleep_for(chrono::milliseconds(500));}}void decrement(){for (int i 0; i 10; i){m_value--;cout decrement number: m_value , theadID: this_thread::get_id() endl;this_thread::sleep_for(chrono::milliseconds(500));}}// atomicint atomic_intatomic_int m_value 0; };int main() {Counter c;auto increment bind(Counter::increment, c);auto decrement bind(Counter::decrement, c);thread t1(increment);thread t2(decrement);t1.join();t2.join();return 0; } 通过代码的对比可以看出使用了原子变量之后就不需要再定义互斥量了在使用上更加简便并且这两种方式都能保证在多线程操作过程中数据的正确性不会出现数据的混乱。 原子类型atomicT 可以封装原始数据最终得到一个原子变量对象操作原子对象能够得到和操作原始数据一样的效果当然也可以通过store()和load()来读写原子对象内部的原始数据。 3. 效率的对比 3.1 原子变量版本 #includeiostream #includememory #includethread #includemutex #includecondition_variable #includefunctional #includeatomicstd::atomicint shared_data; void func() {for (int i 0; i 100000; i){shared_data;} }int main() {auto last std::chrono::duration_caststd::chrono::microseconds(std::chrono::system_clock::now().time_since_epoch()).count();std::thread t1(func);std::thread t2(func);t1.join();t2.join();shared_data.store(666);auto cnt shared_data.load();std::cout shared_data cnt std::endl;auto cur std::chrono::duration_caststd::chrono::microseconds(std::chrono::system_clock::now().time_since_epoch()).count();std::cout cur - last std::endl;return 0; } 执行的结果为 shared_data666 4447 其中消耗的微妙数为3000到5000。  3.2 互斥锁版本 #includeiostream #includememory #includethread #includemutex #includecondition_variable #includefunctional #includeatomicint shared_data; std::mutex mtx; void func() {for (int i 0; i 100000; i){std::lock_guardstd::mutex locker(mtx);shared_data;} }int main() {auto last std::chrono::duration_caststd::chrono::microseconds(std::chrono::system_clock::now().time_since_epoch()).count();std::thread t1(func);std::thread t2(func);t1.join();t2.join();std::cout shared_data shared_data std::endl;auto cur std::chrono::duration_caststd::chrono::microseconds(std::chrono::system_clock::now().time_since_epoch()).count();std::cout cur - last std::endl;return 0; }执行结果为 shared_data200000 14698 其中消耗的微妙数为12000到20000。  对比两种方式的消耗时间发现使用原子量的效率更高 本文参考原子变量 | 爱编程的大丙 (subingwen.cn)
http://www.pierceye.com/news/389793/

相关文章:

  • 织梦贷款网站源码备案过的网站换域名
  • 南宁网站seo顾问广州正规的免费建站
  • 温州教育网站建设建网站 陕西牛人网络科技
  • 青岛网站建设eoe世界杯现场直播
  • com网站建设中社交app定制开发
  • 阅读网站建设简易做海报网站
  • 上海企业网站建设推荐wordpress登录插件
  • 做网站的人联系电话可做产品预售的网站
  • 内部网络网站怎么做微信公众号推文制作软件
  • 什么网站做adsense好酒店官方网站建设书
  • 国家城乡与住房建设部网站做海报有什么参考的网站
  • 建德营销型网站建设企业邮箱地址怎么填写
  • 网站建设规划书总结怎么写wordpress 菜单链接
  • 网站目录程序重庆市建筑工程造价信息网官网
  • WordPress上传后找不到seo优化工程师
  • wordpress登录界面插件如何优化培训方式
  • 贵州建网站网站怎么优化自己免费
  • 个人网站介绍源码网站制作昆山
  • 网站开发属于知识产权吗如何制作软件程序
  • 外贸网站模板源码网络架构如何写
  • 网站建设美工的职位要求建材做哪些网站好
  • 淘宝客自建网站wap免费空间
  • python快速搭建网站如何制作一个微信刷题小程序
  • 域名备案与网站备案网站漏洞解决
  • 公明做网站多少钱高端网站设计怎么样
  • 重庆网站建设 熊掌号wordpress 被发文章
  • 网站建设管理视频广东省农业农村厅班子
  • 网站建设需要钱吗开发一个小程序流程
  • 通州富阳网站建设做足彩网站推广
  • 一个公司做网站需要注意什么条件车之家汽车官网