竞争条件算法

发布于 2024-10-13 05:31:48 字数 277 浏览 4 评论 0原文

作为替代实现,建议使用 下面的代码片段有一个共享的 int 变量 转,初始化为0。两个线程都有本地int 常数我分别设置为0和1。

while (turn != mine);
critical_region();
turn = (1-mine);

清楚地解释任何可能的问题和限制 这第二种选择。

我只是不明白“转=(1-我的)”部分 我的意思是,最初,T1 将进入,因为“我的”是 1。 当它退出时,turn仍将保持为0。所以T0,将永远无法进入 临界区?

As an alternative implementation, it is proposed to use the
following code fragment which has a shared int variable
turn, initialised to 0. The two threads have the local int
constant mine set to 0 and 1 respectively.

while (turn != mine);
critical_region();
turn = (1-mine);

Explain clearly any possible problems and restrictions with
this second alternative.

i just dont get the part, "turn = (1-mine)"
i mean initially, T1 will enter cause "mine" is 1.
when it exits, turn will still remain 0. so T0, won't never be able to enter the
critical region?

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

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

发布评论

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

评论(2

感情洁癖 2024-10-20 05:31:48

turn = (1-mine) 相对容易解释。假设线程 0(mine 设置为 0 的线程)具有临界区。完成后,它将把 turn 设置为 1 - mine 或 1,让另一个线程运行。

当线程 1(mine 设置为 1 的线程)具有临界区时。完成后,它将把 turn 设置为 1 - mine 或 0,让另一个线程运行。

这实际上是一个令牌传递系统,其中每个线程通过设置变量来允许另一个线程运行。

此类系统的主要缺点是它是一个令牌传递系统。它不能很好地扩展到更多线程,因为令牌总是传递到特定线程。

举例来说,假设线程 0 已完成令牌处理,因此将 turn 设置为 1。但是线程 1 正在执行一些密集计算,并且不需要令牌现在。

但线程 2确实需要该令牌,因为它想要执行一些临界区工作。它必须等到线程 1 将令牌传递给它,这不是一个好情况。

但事实上,这对于两个线程来说甚至是一个问题。一旦您传递了令牌,您就无法重新进入临界区,直到另一个线程将令牌返回给您,无论它是否确实需要它。

turn = (1-mine) is relatively easy to explain. Let's say thread 0 (the one where mine is set to 0) has the critical section. When it's finished, it will set turn to 1 - mine, or 1, letting the other thread run.

When thread 1 (the one where mine is set to 1) has the critical section. When it's finished, it will set turn to 1 - mine, or 0, letting the other thread run.

This is actually a token-passing system where each thread allows the other to run by setting the variable.

The main disadvantage of something like this is that it is a token passing system. It doesn't scale well to more threads since the token is always passed to a specific thread.

By way of example, let's say thread 0 has finished with the token so sets turn to 1. But thread 1 is off doing some intensive calculations and doesn't need the token right now.

But thread 2 does need the token since it wants to do some critical section work. It has to wait until thread 1 passes it the token, not a good situation to be in.

But, in fact, that's even a problem for two threads. Once you've passed the token, you cannot re-enter the critical section until the other thread gives you the token back, regardless of whether or not it actually needs it.

浅笑依然 2024-10-20 05:31:48

只是不明白“turn = (1-mine)”部分,我的意思是最初,T1会进入,因为“mine”是1。当它退出时,turn仍将保持为0。所以T0,永远不会是能够进入临界区吗?

首先,该代码的格式掩盖了一个令人讨厌的把戏 - 最好将其写成:

while (turn != mine)
    ; //spin
critical_region();
turn = (1-mine);

因此需要注意的重要一点是,当 turn == mine 时,线程将继续并进行出计算。

因此,我们将线程称为 T0 和 T1。

当 T0 完成时,turn 将设置为什么?

当 T1 完成时,turn 将设置为什么?

just dont get the part, "turn = (1-mine)" i mean initially, T1 will enter cause "mine" is 1. when it exits, turn will still remain 0. so T0, won't never be able to enter the critical region?

First of all, the formatting of that code disguises a nasty trick - it might be better to write it like:

while (turn != mine)
    ; //spin
critical_region();
turn = (1-mine);

So the important thing to note is that when turn == mine, the thread will proceed and carry out the computation.

So, let's call our threads T0 and T1.

When T0 finishes, what will turn be set to?

When T1 finishes, what will turn be set to?

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