gpt4 book ai didi

java - 竞争条件 : from harmful to harmless

转载 作者:行者123 更新时间:2023-11-29 05:55:45 25 4
gpt4 key购买 nike

<分区>

我从 everything2.com 中采用了竞争条件的定义如下:

A race condition is a situation where running multiple concurrent processes (for these purposes, a thread is also modeled as a process, as are processes running on separate machines) will give differing results, depending on the (unspecified, and usually unspecifiable) details of the ordering of operations.

现在考虑以下来自 Chapter 3: Race Conditions and Mutual Exclusion 的示例:

The most dangerous race conditions, however, involve access to shared data structures. If two threads are updating the same data structure at the same time, the changes may be made partially by one thread and partially by the other. The contents of the data structure can then become garbled, which will confuse threads that access it later, thereby causing them to crash.

显然,此示例包含有害的竞争条件。

有一个解决方案可以避免这种有害的竞争条件:

The trick is to force the threads to access the data structure one at a time, so called mutual exclusion, so that each thread can complete its update and leave the structure in a consistent state for the next thread.

现在根据上面的定义,上面的解决方案中仍然存在竞争条件:

If thread A makes change first, then final content of shared data structure is filled by thread B; if thread B makes change first, then final content of shared data structure is filled by thread A.

但现在它是一个无害的竞争条件。

从这个示例中,我得到以下结论:

A race condition is harmful if and only if it is avoidable.

我不确定上述结论是双向的还是只有一个方向是正确的还是不正确的。那么有没有样本可以反驳这个结论呢?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com