在多线程环境中,什么是忙碌的自旋? [英] What is busy spin in a multi-threaded environment?

查看:106
本文介绍了在多线程环境中,什么是忙碌的自旋?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在多线程环境中什么是忙碌自旋"?

What is "Busy Spin" in multi-threaded environment?

它如何有用以及如何在Java中的多线程环境中实现?

How it is useful and how can it be implemented in java in a multi-threaded environment?

以什么方式对提高应用程序的性能有用?

In what way can it be useful in improving the performance of an application?

推荐答案

其他一些答案错过了忙碌等待的真正问题.

Some of the other answers miss the real problem with busy waiting.

除非您在谈论的是节省电的应用程序,否则消耗CPU时间本身并不是一件坏事.只有当有其他线程或进程可以运行时,这才是不好的.当准备好运行的线程之一是您的busy-wait循环正在等待的线程时,这真的很糟糕.

Unless you're talking about an application where you are concerned with conserving electrical power, then burning CPU time is not, in and of itself, a Bad Thing. It's only bad when there is some other thread or process that is ready-to-run. It's really bad when one of the ready-to-run threads is the thread that your busy-wait loop is waiting for.

那是真正的问题.在普通操作系统上运行的普通用户模式程序无法控制哪些线程在哪些处理器上运行,普通操作系统无法分辨忙于等待的线程与正在执行工作的线程之间的区别,即使OS知道线程正在忙于等待,它也无法知道线程在等待什么.

That's the real issue. A normal, user-mode program running on a normal operating system has no control over which threads run on which processors, a normal operating system has no way to tell the difference between a thread that is busy waiting and a thread that is doing work, and even if the OS knew that the thread was busy-waiting, it would have no way to know what the thread was waiting for.

因此,繁忙的服务员完全有可能等待许多毫秒(实际上是一个永恒的时间),等待一个事件,而使事件发生的唯一线程位于边线(即,在运行队列中) )等待轮到使用CPU.

So, it's entirely possible for the busy waiter to wait for many milliseconds (practically an eternity), waiting for an event, while the the only thread that could make the event happen sits on the sideline (i.e., in the run queue) waiting for its turn to use a CPU.

繁忙等待通常用于对哪些线程在哪些处理器上运行进行严格控制的系统中.当您知道知道将导致该事件的线程实际上正在其他处理器上运行时,繁忙等待可能是等待事件的最有效方法.当您正在为操作系统本身编写代码,或者正在编写在实时操作系统下运行的嵌入式实时应用程序时,通常就是这种情况.

Busy waiting is often used in systems where there is tight control over which threads run on which processors. Busy waiting can be the most efficient way to wait for an event when you know that the thread that will cause it is actually running on a different processor. That often is the case when you're writing code for the operating system itself, or when you're writing an embedded, real-time application that runs under a real-time operating system.

凯文·沃尔特斯(Kevin Walters)谈到了等待时间非常短的情况.可以允许在普通OS上运行的,与CPU绑定的普通程序在每个时间片中执行数百万条指令.因此,如果程序使用自旋锁来保护仅由几条指令组成的关键部分,则任何线程在关键部分中丢失其时间片的可能性极小.这意味着,如果线程A发现自旋锁被锁定,则持有该锁的线程B很可能实际上在另一个CPU上运行.因此,当您知道它将在多处理器主机上运行时,可以在普通程序中使用自旋锁.

Kevin Walters wrote about the case where the time to wait is very short. A CPU-bound, ordinary program running on an ordinary OS may be allowed to execute millions of instructions in each time slice. So, if the program uses a spin-lock to protect a critical section consisting of just a few instructions, then it is highly unlikely that any thread will lose its time slice while it is in the critical section. That means, if thread A finds the spin-lock locked, then it is highly likely that thread B, which holds the lock, actually is running on a different CPU. That's why it can be OK to use spin-locks in an ordinary program when you know it's going to run on a multi-processor host.

这篇关于在多线程环境中,什么是忙碌的自旋?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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