我们可以在单线程程序中有竞争条件吗? [英] Can we have race conditions in a single-thread program?

查看:141
本文介绍了我们可以在单线程程序中有竞争条件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以在这里找到关于什么是竞态条件的非常好的解释。

You can find on here a very good explanation about what is a race condition.

我最近看到很多人对比赛条件和线程做出混乱的说法。

I have seen recently many people making confusing statements about race conditions and threads.

条件只能发生在线程之间。但我看到的代码看起来像竞态条件,在事件和异步语言,即使程序是单线程,如在Node.js,GTK +等。

I have learned that race conditions could only occur between threads. But I saw code that looked like race conditions, in event and asynchronous based languages, even if the program was single thread, like in Node.js, in GTK+, etc.

推荐答案

所有示例都使用虚构语言到Javascript。

All examples are in a fictional language very close to Javascript.

短:


  1. 竞争条件只能在两个或多个线程之间发生。我们不能在单个线程过程中拥有争用条件(例如在单个线程中,非I / O执行程序)。

  1. A race condition can only occur between two or more threads. We cannot have race conditions inside a single thread process (for example in a single thread, non I/O doing program).

但是在许多情况下,单线程程序可以:

But a single thread program can in many cases :


  1. 给出看起来类似于竞态条件的情况,

  1. give situations which looks similar to race conditions, like in event based program with an event loop, but are not real race conditions

在其他主题之间或与其他主题之间触发一个竞争条件,但是不是真正的竞争条件<示例:

trigger a race condition between or with other thread(s), for example :


  1. 其他程序,如客户端

  2. 库线程或服务器





或更多的线程



竞争条件只能发生在两个或多个线程尝试访问共享资源时,如果不知道同时被未知的压缩。这会产生未确定的结果。 (这很重要。)

I) Race conditions can only occur between two or more threads

A race condition can only occur when two or more threads try to access a shared resource without knowing it is modified at the same time by unknown insctructions from the other thread(s). This gives an undetermined result. (This is really important.)

单线程过程只是一个已知指令序列,因此会产生

A single thread process is nothing more than a sequence of known instructions which therefore results in a determined result, even if the execution order of instructions is not easy to read in the code.

许多编程语言通过事件循环处理的信号,它检查事件队列并触发侦听器。这个例子是Javascript,libuevent,reactPHP,GNOME GLib ...有时,我们可以找到似乎是竞态条件的情况,但不是

Many programming languages implements asynchronous programming features through events or signals, handled by a main loop or event loop which check for the event queue and trigger the listeners. Example of this are Javascript, libuevent, reactPHP, GNOME GLib... Sometimes, we can find situations which seems to be race conditions, but they are not.

事件循环的调用方式总是已知,因此结果确定,即使指令的执行顺序不易阅读(或

The way the event loop is called is always known, so the result is determined, even if the execution order of instructions is not easy to read (or even cannot be read if we do not know the library).

例如:

setTimeout(
  function() { console.log("EVENT LOOP CALLED"); },
  1
); // We want to print EVENT LOOP CALLED after 1 milliseconds

var now = new Date();
while(new Date() - now < 10) //We do something during 10 milliseconds

console.log("EVENT LOOP NOT CALLED");

总是(可以在node.js中测试) :

in Javascript output is always (you can test in node.js) :

EVENT LOOP NOT CALLED
EVENT LOOP CALLED

因为,当堆栈为空(所有函数都已返回)时调用事件循环。

because, the event loop is called when the stack is empty (all functions have returned).

意识到这只是一个例子,在以不同方式实现事件的语言中,结果可能不同,但它仍然由实现决定。

Be aware that this is just an example and that in languages that implements events in a different way, the result might be different, but it would still be determined by the implementation.

如果其他进程请求我们的进程,我们的程序不会以原子方式处理请求,并且我们的进程在请求之间共享一些资源,客户端之间可能存在竞争条件

If other processes are requesting our process, that our program do not treat requests in an atomic way, and that our process share some resources between the requests, there might be a race condition between clients.

示例:

var step;
on('requestOpen')(
  function() {
    step = 0;
  }
);

on('requestData')(
  function() {
    step = step + 1;
  }
);

on('requestEnd')(
  function() {
    step = step +1; //step should be 2 after that
    sendResponse(step);
  }
);

这里,我们有一个经典的竞争条件设置。如果一个请求在另一个结束之前被打开,则步骤将被重置为0.如果两个 requestData requestEnd 因为两个并发请求,步骤将达到3. 但这是因为我们将事件序列视为未确定。

Here, we have a classical race condition setup. If a request is opened just before another ends, step will be reset to 0. If two requestData events are triggered before the requestEnd because of two concurrent requests, step will reach 3. But this is because we take the sequence of events as undetermined. We expect that the result of a program is most of the time undetermined with an undetermined input.

事实上,如果我们的程序是单线程,给出一系列事件

In fact, if our program is single thread, given a sequence of events the result is still always determined. The race condition is between clients.

有两种方式可以理解这件事:

There is two ways to understand the thing :


  • 我们可以将客户视为我们计划的一部分(为什么不?),在这种情况下,我们的计划是多线程。

  • 更常见的是,我们可以认为客户不是我们计划的一部分。在这种情况下,它们只是输入。当我们考虑一个程序是否有确定的结果时,我们使用给定的输入来执行。否则,即使最简单的程序返回输入; 也会有未确定的结果。

  • We can consider clients as part of our program (why not ?) and in this case, our program is multi thread. End of the story.
  • More commonly we can consider that clients are not part of our program. In this case they are just input. And when we consider if a program has a determined result or not, we do that with input given. Otherwise even the simplest program return input; would have a undetermined result.

注意:


  • 如果我们的进程以原子方式处理请求,

  • 如果我们可以识别请求并将变量附加到请求对象,该请求对象在请求的每个步骤都是相同的,则不存在任何争用条件。共享资源

在我们的程序中,我们经常使用产生其他进程或线程的库,或者只是与其他进程进行I / O(和I / O总是不确定的)。

In our programs, we often use libraries which spawn other processes or threads, or that just do I/O with other processes (and I/O is always undetermined).

示例:

databaseClient.sendRequest('add Me to the database');

databaseClient.sendRequest('remove Me from the database');

这可以在异步库中触发竞争条件。这是如果 sendRequest()在将请求发送到数据库之后,但在请求真正执行之前返回的情况。我们立即发送另一个请求,我们不知道第一个将在第二个被评估之前被执行,因为数据库在另一个线程上工作。

This can trigger a race condition in an asynchronous library. This is the case if sendRequest() returns after having sent the request to the database, but before the request is really executed. We immediately send another request and we cannot know if the first will be executed before the second is evaluated, because database works on another thread. There is a race condition between the program and the database process.

但是,如果数据库与程序在同一个线程上在现实生活中不发生经常)是sendRequest在处理请求之前返回是不可能的。 (除非请求排队,但在这种情况下,结果仍是确定,因为我们知道如何以及何时读取队列。)

But, if the database was on the same thread as the program (which in real life does not happen often) is would be impossible that sendRequest returns before the request is processed. (Unless the request is queued, but in this case, the result is still determined as we know exactly how and when the queue is read.)

简单来说,单线程程序不能免于trigerring的竞态条件。但是它们只能在外部程序的其他线程之间发生 。我们的程序的结果可能不确定,因为我们的程序从其他程序接收的输入未确定。

In short, single-thread programs are not free from trigerring race conditions. But they can only occur with or between other threads of external programs. The result of our program might be undetermined, because the input our program receive from those other programs is undetermined.

这篇关于我们可以在单线程程序中有竞争条件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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