在多线程应用程序中使用屏障的真实示例是什么?

发布于 2024-08-13 10:06:12 字数 94 浏览 12 评论 0原文

JDK 的并发包、Boost 的线程库、Perl 的线程库(尽管不在 Python 中)都实现了屏障,我还没有遇到使用屏障的需要,所以想知道多线程应用程序中的典型用例是什么。

JDK's concurrency package, Boost's thread library, Perl's Thread library (not in Python though) all implement barrier, I haven't come across a need for using a barrier, so wondering what would a typical use case be in multi-threaded applications.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋心╮凉 2024-08-20 10:06:12

可以通过人为的示例在任何地方使用屏障,但您经常会在分散/减少方法中看到它们,其中在继续之前都需要不同线程的结果。

例如,如果你想并行排序,你可以将列表拆分 n 次并启动 n 个线程来对它们的部分进行排序并暂停,当它们全部完成时,它们会死掉,让父级知道终于可以合并排序的块了。 (我知道有更好的方法,但这只是一种实现)。

我看到的另一个地方是并行网络,您必须为每个有效负载发送一定量的数据。因此,接口将启动 n 个桶并等待它们全部填满,然后再发送传输。当您考虑分区 T1 线路时,​​它有点有意义,通过 64 个多路复用分区发送一个突发数据会比发送 1 个分区的数据更好(这基本上成本相同,因为数据包必须用 0 填充。)

希望那些有一些事情让你思考这个问题!

Barriers can be used all over the place through contrived examples but you'll often seem them in a scatter/reduce method where the results of the different threads are all required before proceeding.

If you wanted to parallelize a sort for instance you could split the list n times and start n threads to sort their section and pause, when they're all finished they would die letting the parent know that it's finally okay to combine the sorted chunks. (I know there are better ways but it's one implementation).

The other place I've seen it is in parallelized networking where you have to send a certain amount of data per payload. So the interface will start up n buckets and wait for them all to fill before sending out the transmission. When you think about a partitioned T1 line it sorta makes sense, sending one burst of data over the 64 multiplexed partitions would be better than sending 1 partition's data (which essentially costs the same since the packet has to be padded with 0's.)

Hope those are some things to get you thinking about it the problem!

少女情怀诗 2024-08-20 10:06:12

示例:一组线程同时工作以计算结果集,并且需要将所述结果集(部分/全部)作为下一阶段处理某些/所有线程的输入“障碍”。

屏障可以更轻松地同步多个线程,而无需围绕多个条件制定解决方案。 互斥体

但我不能说我经常看到障碍。在某些时候,随着线程数量的增长,可能值得考虑使用更加“解耦”的系统来管理可能的死锁。

Example: a set of threads work concurrently to compute a result-set and the said result-set (in part/total) is required as input for the next stage of processing to some/all threads at the "barrier".

A barrier makes it easier to synchronize multiple threads without having to craft a solution around multiple conditions & mutexes.

I can't say I have seen barriers often though. At some point, as the number of threads grows, it might be worthwhile considering a more "decoupled" system as to manage possible dead-locks.

不疑不惑不回忆 2024-08-20 10:06:12

MSDN:Barrier 是一个对象,它阻止并行操作中的各个任务继续执行,直到所有任务都到达屏障。当并行操作发生时,它很有用
阶段,每个阶段都需要任务之间的同步。

在此处找到

MSDN: A Barrieris an object that prevents individual tasks in a parallel operation from continuing until all tasks reach the barrier. It is useful when a parallel operation occurs in
phases, and each phase requires synchronization between tasks.

Found here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文