返回介绍

Mechanisms

发布于 2024-10-11 20:33:59 字数 6501 浏览 0 评论 0 收藏 0

A race condition happens when two sections of code that are designed to be executed in a sequence get executed out of sequence. To understand how this works, you need to first understand the concept of concurrency. In computer science, concurrency is the ability to execute different parts of a program simultaneously without affecting the outcome of the program. Concurrency can drastically improve the performance of programs because different parts of the program’s operation can be run at once.

当两个被设计按顺序执行的代码部分在不按顺序执行时,发生了竞态条件。要理解这个过程,首先需要了解并发的概念。在计算机科学中,并发是指能够同时执行程序的不同部分而不影响程序的结果。由于程序的不同部分可以同时运行,因此并发可以大大提高程序的性能。

Concurrency has two types: multiprocessing and multithreading. Multiprocessing refers to using multiple central processing units ( CPUs ) , the hardware in a computer that executes instructions, to perform simultaneous computations. On the other hand, multithreading is the ability of a single CPU to provide multiple threads , or concurrent executions. These threads don’t actually execute at the same time; instead, they take turns using the CPU’s computational power. When one thread is idle, other threads can continue taking advantage of the unused computing resources. For example, when one thread is suspended while waiting for user input, another can take over the CPU to execute its computations.

并发有两种类型:多处理和多线程。多处理是指使用多个中央处理单元(CPU),计算机中执行指令的硬件,以执行同时计算。另一方面,多线程是单个 CPU 提供多个线程或并发执行的能力。这些线程实际上不同时执行;相反,它们轮流使用 CPU 的计算能力。当一个线程空闲时,其他线程可以继续利用未使用的计算资源。例如,当一个线程被暂停等待用户输入时,另一个线程可以接管 CPU 执行其计算。

Arranging the sequence of execution of multiple threads is called scheduling . Different systems use different scheduling algorithms, depending on their performance priorities. For example, some systems might schedule their tasks by executing the highest-priority tasks first, while another system might execute its tasks by giving out computational time in turns, regardless of priority.

安排多个线程的执行顺序称为调度。不同的系统使用不同的调度算法,这取决于它们的性能优先级。例如,一些系统可能按照最高优先级任务的顺序安排任务执行,而另一个系统可能按照轮流分配计算时间的方式执行任务,不考虑优先级。

This flexible scheduling is precisely what causes race conditions. Race conditions happen when developers don’t adhere to certain safe concurrency principles, as we’ll discuss later in this chapter. Since the scheduling algorithm can swap between the execution of two threads at any time, you can’t predict the sequence in which the threads execute each action.

弹性调度正是导致竞争条件发生的原因。竞争条件发生在开发人员不遵守某些安全并发原则时,我们将在本章后面讨论。由于调度算法可以在任何时间之间交换两个线程的执行,因此无法预测线程执行每个动作的顺序。

To see why the sequence of execution matters, let’s consider an example (courtesy of Wikipedia: https://en.wikipedia.org/wiki/Race_condition ). Say two concurrent threads of execution are each trying to increase the value of a global variable by 1. If the variable starts out with a value of 0, it should end up with a value of 2. Ideally, the threads would be executed in the stages shown in Table 12-1 .

为了看到执行顺序的重要性,让我们考虑一个示例(由维基百科提供:https://en.wikipedia.org/wiki/Race_condition)。假设有两个并发的执行线程,每个线程都试图将全局变量的值增加 1。如果变量的初始值为 0,则应该最终增加到 2。理想情况下,线程应该按照表 12-1 中显示的阶段执行。

Table 12-1 : Normal Execution of Two Threads Operating on the Same Variable

表 12-1:两个线程在同一变量上正常执行

Thread 1Thread 2Value of variable A
Stage 1  0
Stage 2Read value of A 0
Stage 3Increase A by 1 0
Stage 4Write the value of A 1
Stage 5 Read value of A1
Stage 6 Increase A by 11
Stage 7 Write the value of A2

But if the two threads are run simultaneously, without any consideration of conflicts that may occur when accessing the same resources, the execution could be scheduled as in Table 12-2 instead.

但是如果两个线程同时运行,没有考虑到访问相同资源时可能发生的冲突,执行可能会按照表 12-2 中的计划安排。

Table 12-2 : Incorrect Calculation Due to a Race Condition

表格 12-2:由于竞争条件而导致的错误计算。

Thread 1Thread 2Value of variable A
Stage 1  0
Stage 2Read value of A 0
Stage 3 Read value of A0
Stage 4Increase A by 1 0
Stage 5 Increase A by 10
Stage 6Write the value of A 1
Stage 7 Write the value of A1

In this case, the final value of the global variable becomes 1, which is incorrect. The resulting value should be 2.

在这种情况下,全局变量的最终值变为 1,这是不正确的。结果值应该是 2。

In summary, race conditions happen when the outcome of the execution of one thread depends on the outcome of another thread, and when two threads operate on the same resources without considering that other threads are also using those resources. When these two threads are executed simultaneously, unexpected outcomes can occur. Certain programming languages, such as C/C++, are more prone to race conditions because of the way they manage memory.

总之,当一个线程的执行结果取决于另一个线程的结果,并且两个线程在没有考虑其他线程也在使用这些资源的情况下操作同一资源时,就会发生竞争条件。 当这两个线程同时执行时,可能会发生意外的结果。 由于它们管理内存的方式,某些编程语言(如 C / C ++)更容易发生竞争条件。

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文