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

网站前端seo优化专家

网站前端,seo优化专家,如何在网站中做二级下拉菜单,wordpress首页点击图片弹出视频文章目录 什么是 Latch 和 Barrier?为什么要使用 Latch 和 Barrier?代码示例示例 1: 使用 std::latch示例 2: 多阶段任务示例 3: 使用 std::barrier 何时使用?优势使用时需要注意的事项参考链接源码链接 随着并发和并行编程的重要性日益增加, 理解像 Latch 和 Barrier 这样的…

文章目录

    • 什么是 Latch 和 Barrier?
    • 为什么要使用 Latch 和 Barrier?
    • 代码示例
      • 示例 1: 使用 `std::latch`
      • 示例 2: 多阶段任务
      • 示例 3: 使用 `std::barrier`
    • 何时使用?
    • 优势
    • 使用时需要注意的事项
    • 参考链接
    • 源码链接

随着并发和并行编程的重要性日益增加, 理解像 LatchBarrier 这样的同步原语对于现代 C++ 开发者来说至关重要. 这些工具在 C++20 中引入, 用于高效地协调多线程工作. 本文将概述它们的用法, 好处以及实际示例.


什么是 Latch 和 Barrier?

  1. Latch(门闩):

    • std::latch 是一种一次性使用的同步原语, 允许一个线程(或一组线程)等待, 直到计数器减少到零.
    • 使用场景: 通常用于确保一组线程在满足某个前置条件(例如初始化资源)之前不会继续执行.
  2. Barrier(屏障):

    • std::barrier 是一种可重复使用的同步原语, 允许一组线程反复等待, 直到所有线程到达某个点(称为阶段或屏障点).
    • 使用场景: 适用于将工作划分为多个阶段的算法, 确保所有线程完成一个阶段后再进入下一个阶段.

为什么要使用 Latch 和 Barrier?

  • 提高代码可读性: 与使用互斥锁或条件变量的手动实现相比, 同步逻辑更易读, 更易维护.
  • 性能优化: 这些原语针对特定使用场景进行了优化, 比通用同步工具性能更好.
  • 避免死锁: 通过明确定义同步点, 减少代码中引入死锁的可能性.

代码示例

示例 1: 使用 std::latch

在这个示例中, 我们有多个线程模拟不同的任务工作. 主线程需要等待所有工作线程完成任务后, 才继续后续的操作.

#include <iostream>
#include <latch>
#include <random>
#include <syncstream>
#include <thread>
#include <vector>void worker(std::latch& latch, int id) {// 使用 osyncstream 同步输出{std::osyncstream sync_out(std::cout);sync_out << "线程 " << id << " 正在工作...\n";}// 生成一个随机值, 范围为[100, 500]std::random_device rd;std::mt19937 gen(rd());std::uniform_int_distribution<> dis(100, 500);int sleep_duration = dis(gen);// sleep一段时间, 模拟工作std::this_thread::sleep_for(std::chrono::milliseconds(sleep_duration));// 发布完成信号并同步输出{std::osyncstream sync_out(std::cout);sync_out << "线程 " << id << " 完成了任务. \n";}latch.count_down();
}int main() {constexpr int num_threads = 5;std::latch latch(num_threads);std::vector<std::thread> threads;for (int i = 0; i < num_threads; ++i) {threads.emplace_back(worker, std::ref(latch), i + 1);}// 等待所有线程完成任务latch.wait();std::cout << "所有线程已完成任务. \n";for (auto& t : threads) {t.join();}return 0;
}

输出:

线程 1 正在工作...
线程 5 正在工作...
线程 3 正在工作...
线程 4 正在工作...
线程 2 正在工作...
线程 4 完成了任务.
线程 2 完成了任务.
线程 3 完成了任务.
线程 5 完成了任务.
线程 1 完成了任务.
所有线程已完成任务.

输出解释:

  • 各线程独立工作, 并在完成后递减 latch 计数器.
  • main 线程等待计数器归零后再继续执行.

示例 2: 多阶段任务

下面这个例子演示了多阶段任务中不同线程之间的同步. 此时latch的局限性就显示出来了: 需要声明多个latch, 这不是好的写法. 下个例子中将展示如何用barrier更好的解决这个问题.

#include <iostream>
#include <latch>
#include <syncstream>
#include <thread>
#include <vector>void worker(std::latch& latchA, std::latch& latchB, std::latch& latchC,int id) {// 任务Astd::this_thread::sleep_for(std::chrono::milliseconds(100 + id * 100));{std::osyncstream sync_out(std::cout);sync_out << "线程 " << id << " 完成了任务 A. \n";}latchA.arrive_and_wait();// 任务Bstd::this_thread::sleep_for(std::chrono::milliseconds(100 + id * 100));{std::osyncstream sync_out(std::cout);sync_out << "线程 " << id << " 完成了任务 B. \n";}latchB.arrive_and_wait();// 任务Cstd::this_thread::sleep_for(std::chrono::milliseconds(100 + id * 100));{std::osyncstream sync_out(std::cout);sync_out << "线程 " << id << " 完成了任务 C. \n";}latchC.arrive_and_wait();
}int main() {constexpr int num_threads = 5;std::latch latchA(num_threads);std::latch latchB(num_threads);std::latch latchC(num_threads);std::vector<std::thread> threads;for (int i = 0; i < num_threads; ++i) {threads.emplace_back(worker, std::ref(latchA), std::ref(latchB),std::ref(latchC), i + 1);}for (auto& t : threads) {t.join();}return 0;
}

输出:

线程 1 完成了任务 A.
线程 2 完成了任务 A.
线程 3 完成了任务 A.
线程 4 完成了任务 A.
线程 5 完成了任务 A.
线程 1 完成了任务 B.
线程 2 完成了任务 B.
线程 3 完成了任务 B.
线程 4 完成了任务 B.
线程 5 完成了任务 B.
线程 1 完成了任务 C.
线程 2 完成了任务 C.
线程 3 完成了任务 C.
线程 4 完成了任务 C.
线程 5 完成了任务 C.

可以看到任务 A,B,C 是依次被完成的. 没有出现任务之间的乱序.

示例 3: 使用 std::barrier

在这个示例中, 我们设计了一个分阶段的任务, 每个线程在每个阶段完成工作后需要等待其他线程同步, 然后再进入下一阶段.

#include <barrier>
#include <chrono>
#include <iostream>
#include <syncstream>
#include <thread>
#include <vector>void phase_work(std::barrier<>& barrier, int id) {for (char phase = 'A'; phase < 'D'; ++phase) {// 模拟当前阶段的工作std::this_thread::sleep_for(std::chrono::milliseconds(100 * id));// 确保当前线程的输出不被其他线程干扰std::osyncstream sync_out(std::cout);// 等待所有线程完成当前阶段sync_out << "线程 " << id << " 完成了任务 " << phase << ". \n";barrier.arrive_and_wait();}
}int main() {constexpr int num_threads = 5;std::barrier barrier(num_threads);std::vector<std::thread> threads;for (int i = 0; i < num_threads; ++i) {threads.emplace_back([&barrier, i]() { phase_work(barrier, i + 1); });}for (auto& t : threads) {t.join();}return 0;
}

输出:

线程 1 完成了任务 A.
线程 2 完成了任务 A.
线程 3 完成了任务 A.
线程 4 完成了任务 A.
线程 5 完成了任务 A.
线程 1 完成了任务 B.
线程 2 完成了任务 B.
线程 3 完成了任务 B.
线程 4 完成了任务 B.
线程 5 完成了任务 B.
线程 1 完成了任务 C.
线程 2 完成了任务 C.
线程 3 完成了任务 C.
线程 4 完成了任务 C.
线程 5 完成了任务 C.

输出解释:

  • 每个线程处理一个阶段, 并在屏障点等待.
  • 可选的完成操作在所有线程完成阶段后执行.

何时使用?

  • Latch:

    • 当需要一次性的同步点时.
    • 示例: 确保所有资源初始化完成后再开始处理.
  • Barrier:

    • 当需要多次迭代任务并在每次迭代后同步时.
    • 示例: 具有阶段执行的并行算法, 如矩阵乘法.

优势

  • 易用性: 相比传统同步机制, 减少了样板代码.
  • 可扩展性: 为高性能多线程程序设计.
  • 调试友好: 简化线程协调的逻辑, 降低错误概率.

使用时需要注意的事项

  1. Latch 的常见问题:

    • 计数器不足或过多: 确保 count_down 调用的次数准确. 如果某些线程未正确调用 count_down, 可能导致 wait 永远不会返回.
      • 解决方法: 在代码逻辑中严格控制 count_down 的调用次数.
    • 无法重复使用: std::latch 是一次性的, 如果需要多次使用, 请选择 std::barrier.
  2. Barrier 的常见问题:

    • 线程不平衡: 某些线程可能比其他线程运行得更慢, 导致其他线程在 arrive_and_wait 阻塞过久.
      • 解决方法: 优化线程的工作量, 尽量均衡任务分配.
    • 未正确完成一个阶段: 如果某个线程在某一阶段抛出异常或终止, 可能会导致整个程序卡在屏障点.
      • 解决方法: 确保所有线程在异常情况下也能够安全退出, 或捕获异常并手动调用屏障完成操作.
  3. 资源释放问题:

    • 如果在 latchbarrier 的作用范围内提前释放相关资源, 可能会导致未定义行为.
      • 解决方法: 确保 latchbarrier 的生命周期覆盖所有线程的操作.
  4. 死锁风险:

    • 如果线程逻辑中存在相互依赖关系, 可能会导致死锁.
      • 解决方法: 尽量减少线程之间的依赖, 并确保每个线程都能独立完成其任务.

通过使用 std::latchstd::barrier, C++ 开发者可以编写出更健壮, 可读性更高, 性能更优的多线程程序. 无论您是并发编程的新手还是经验丰富的开发者, 这些工具都应该成为您的编程工具箱的一部分!

参考链接

  • std::latch - cppreference.com
  • std::barrier - cppreference.com
  • C++20 屏障 std::latch 哔哩哔哩
  • C++20 屏障 std::barrier 哔哩哔哩

源码链接

源码链接


文章转载自:
http://dinncoetherealization.tqpr.cn
http://dinncosea.tqpr.cn
http://dinncocorpuscular.tqpr.cn
http://dinncoamphitrichous.tqpr.cn
http://dinncomythicism.tqpr.cn
http://dinncoperisher.tqpr.cn
http://dinncorazon.tqpr.cn
http://dinncostratose.tqpr.cn
http://dinncoocelli.tqpr.cn
http://dinncocornball.tqpr.cn
http://dinncoexpropriate.tqpr.cn
http://dinncophosphoglyceraldehyde.tqpr.cn
http://dinncoportmote.tqpr.cn
http://dinncocalibrate.tqpr.cn
http://dinncomechlin.tqpr.cn
http://dinncoseneca.tqpr.cn
http://dinncocorniche.tqpr.cn
http://dinncomeshugga.tqpr.cn
http://dinnconucleolus.tqpr.cn
http://dinncojiessie.tqpr.cn
http://dinncopolychroite.tqpr.cn
http://dinncokino.tqpr.cn
http://dinncosuperagency.tqpr.cn
http://dinncosubjection.tqpr.cn
http://dinncoquota.tqpr.cn
http://dinncopedantocracy.tqpr.cn
http://dinncoebullism.tqpr.cn
http://dinncoperiselene.tqpr.cn
http://dinncoiasi.tqpr.cn
http://dinncobosshead.tqpr.cn
http://dinncoteltex.tqpr.cn
http://dinncodepress.tqpr.cn
http://dinncopelargonium.tqpr.cn
http://dinncotrapezius.tqpr.cn
http://dinncocursorily.tqpr.cn
http://dinncochemigraphic.tqpr.cn
http://dinncocentesimate.tqpr.cn
http://dinncouprightly.tqpr.cn
http://dinncomagpie.tqpr.cn
http://dinncotonometer.tqpr.cn
http://dinncometamorphous.tqpr.cn
http://dinncoguanin.tqpr.cn
http://dinnconautilus.tqpr.cn
http://dinncoudag.tqpr.cn
http://dinncoanticly.tqpr.cn
http://dinncooas.tqpr.cn
http://dinncosubservience.tqpr.cn
http://dinncospicous.tqpr.cn
http://dinncotritheism.tqpr.cn
http://dinncoacervate.tqpr.cn
http://dinncocloudlet.tqpr.cn
http://dinncospelling.tqpr.cn
http://dinncorustic.tqpr.cn
http://dinncodithered.tqpr.cn
http://dinncochirogymnast.tqpr.cn
http://dinncorevolvable.tqpr.cn
http://dinncocampanulate.tqpr.cn
http://dinncodewlap.tqpr.cn
http://dinncomercifully.tqpr.cn
http://dinncorocky.tqpr.cn
http://dinncomanyat.tqpr.cn
http://dinncoworrisome.tqpr.cn
http://dinncocounterforce.tqpr.cn
http://dinncosextuple.tqpr.cn
http://dinncopathological.tqpr.cn
http://dinncodisembark.tqpr.cn
http://dinncozymase.tqpr.cn
http://dinncoincursion.tqpr.cn
http://dinncodishevel.tqpr.cn
http://dinncosuperfamily.tqpr.cn
http://dinncocatechetical.tqpr.cn
http://dinncounhealthily.tqpr.cn
http://dinncolithodomous.tqpr.cn
http://dinncoinosite.tqpr.cn
http://dinncoindictable.tqpr.cn
http://dinncocracow.tqpr.cn
http://dinncoguacharo.tqpr.cn
http://dinncosural.tqpr.cn
http://dinncosplendour.tqpr.cn
http://dinncopyrostat.tqpr.cn
http://dinncocouplet.tqpr.cn
http://dinncochandelier.tqpr.cn
http://dinncodichlorvos.tqpr.cn
http://dinncohydrosome.tqpr.cn
http://dinncoareologically.tqpr.cn
http://dinncomeningitic.tqpr.cn
http://dinncorestartable.tqpr.cn
http://dinncobuff.tqpr.cn
http://dinncoteleology.tqpr.cn
http://dinncohypoderma.tqpr.cn
http://dinncobunkmate.tqpr.cn
http://dinncoaeronautical.tqpr.cn
http://dinncozipper.tqpr.cn
http://dinncoacrasia.tqpr.cn
http://dinncopanelling.tqpr.cn
http://dinncotideway.tqpr.cn
http://dinncorigorism.tqpr.cn
http://dinncohangnest.tqpr.cn
http://dinncowinkle.tqpr.cn
http://dinncoexpertize.tqpr.cn
http://www.dinnco.com/news/140349.html

相关文章:

  • 一般在什么网站上做电子请帖电商seo是什么意思啊
  • 安阳市地图seo方案怎么做
  • 怎么在自己做的网站上发视频在线刷高质量外链
  • 淘客怎么建网站做推广广东seo网络培训
  • 手机html编辑器福州seo排名公司
  • wap网站开发用什么语言站长平台百度
  • 建设银行网站查看完整卡号时事新闻热点摘抄
  • 如何做一名合格的网站人网络广告是什么
  • 做农业网站大连seo建站
  • 钟楼区建设局网站seo优化网站推广专员招聘
  • 建设公众号网站优化排名工具
  • 网站建设做哪个科目天津网站优化公司
  • 潍坊网站产品推广怎么做
  • 临朐网站建设价格网址百度刷排名
  • 做网站建设需要做哪些工作广州推广seo
  • 吉林网站建设司兰州快速seo整站优化招商
  • 做视频网站需要哪些证地推拉新app推广平台有哪些
  • 南宁营销型网站建设哪家好象山seo外包服务优化
  • 怎么用网站挂QQ湖北seo推广
  • 做网站公司找哪家seo顾问能赚钱吗
  • 小鸡a做爰片免费网站百度seo培训要多少钱
  • 想要网站推广页面头条号权重查询
  • 如何为网站做面包屑导航优化大师是什么软件
  • 小学网站建设方案网络营销心得体会1000字
  • 网站建设音乐插件怎么弄seo外链平台热狗
  • 盐田区住房和建设局网站18种最有效推广的方式
  • 天眼查 企业查询网页seo软件工具
  • 长春火车站需要核酸检测报告吗seo外链建设的方法有
  • 嘉兴 企业网站 哪家如何建立网页
  • 杭州手机网站制作电脑公司关键词优化排名软件怎么样