比赛条件如何发挥作用? [英] How can race conditions be useful?

查看:80
本文介绍了比赛条件如何发挥作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

答案之一是关于故意使用竞争条件提及低级别算法的竞争条件的问题.种族条件如何有益?

One of the answers to the question of what race conditions are mentioned low-level algorithms deliberately using race condition. How can race conditions be beneficial?

编辑:并发和队列是一个很好的例子,只要不丢失任何内容,就不必关心事物的排序.关于真正有毛的低级算法是故意这样做的" 的任何想法?

Concurrency and queues are a good example of deliberately not caring about ordering of things, as long as nothing is lost. Any ideas on how "really hairy low-level algorithms do this on purpose"?

推荐答案

并非所有种族都同样糟糕.

Not all races are equally bad.

您能得到的最糟糕的比赛是阅读部分结果.这就是Herb Sutter 所引用的内容称为看到粉红色的大象":您的程序能够观察到违反所有不变量的中间状态.

The worst kind of race that you can get is reading of partial results. This is what Herb Sutter referred to as 'seeing pink elephants': your program is able to observe an intermediate state that violates all invariants.

此处的典型示例是并发非原子写入.如果一个线程从另一个线程同时写入的变量中读取数据,则读取器可能会获得完整的垃圾.您不仅无法分辨读者是看到旧值还是新值,它实际上可能看到一个从未被任何人 写入的值.必须不惜一切代价避免此类数据争用,因为几乎不可能以任何方式推断观察值.例如,在这种情况下,C ++会将您直接带到未定义的行为域.

The typical example here are concurrent non-atomic writes. If one thread reads from a variable that is concurrently written by another thread, the reader might obtain complete garbage. Not only can you not tell whether the reader will see the old value or the new value, it might actually see a value that was never written by anyone. These kinds of data races need to be avoided at all costs, as it's pretty much impossible to reason about the observed values in any way. C++ for example sends you straight to undefined-behavior-land in this case.

一种不太紧要的竞赛是当所有数据访问都是原子访问时,因此您知道读者将只会观察完全写入的值,但是顺序是不确定的.因此,您不知道读取的值实际上是最新的还是一起读取的两个值是否实际上同时在内存中.出于性能原因,接受此选项通常很有用.分布式应用程序是一个突出的例子:通过网络同步数据的速度特别慢,因此通常认为某些节点可能对世界有过时的看法,但仍然能够基于该状态执行工作.考虑一下搜索引擎缓存:基于昨天的缓存给出快速结果比给出缓慢结果或根本不给出结果要好.

A less critical kind of race is when all data accesses are atomic, so you know that readers will only ever observe fully written values, but the order is unspecified. So you don't know whether the value that you read is actually the newest or whether two values that you read together were actually in memory at the same time. It is often useful to accept this for performance reasons. A prominent example here are distributed applications: Synchronizing data over the network is particularly slow, so it is often accepted that certain nodes may have an outdated view of the world but are still able to perform work based on that state. Think of a search engine cache: It's better to give a fast result based on yesterday's cache than to give a slow result or no result at all.

类似的例子发生在非分布式环境中.考虑一个无锁队列:您通常不关心项目在队列中结束的确切顺序.所有生产者竞相"将项目插入队列的末尾,类似地,所有消费者竞合"以消耗队列的前项.但是,只要您可以保证没有任何物品意外丢失或损坏,那么这种降低的控制水平是可以接受的.

Similar examples occur in non-distributed environments. Consider a lock-free queue: You usually don't care about the exact order in which items end up in the queue. All producers 'race' to inserting items to the back of the queue and similarly all consumers 'race' to consume the front item of the queue. However, as long as you can guarantee that no items are accidentally lost or corrupted, this reduced level of control is acceptable.

这篇关于比赛条件如何发挥作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆