问题标签 [busy-waiting]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
linux - select() 是忙等待系统调用吗?
问题:
不管通过什么timeout
论据,
是否select()
占用 CPU 周期,直到任何文件描述符准备好?
java - 并发编程,线程间共享值
我是并发编程的初学者,我想确切地理解为什么当我sleep(1)
在get()
我的第一个想法中评论时这个程序没有结束,是 sleep(1) 将手交还给Main
线程,也许忙等待有有什么关系?
node.js - 在节点中顺序执行
我在nodejs中有一个函数,它的指令必须按顺序执行。我已经尝试了节点 8 和承诺的本机选项(异步和等待)。我不能让他在返回之前等待 FindOne 结果。
对 mongo 的查询很好,我可以恢复文档,但总是在返回后,所以验证不起作用。这个函数应该做的是检查一个集合 {usr, key} 是否已经存在于数据库中
可以是什么?
javascript - 空循环是否太昂贵?
例如在 JavaScript 中,我想观察一个变量并阻止控件,直到变量被更改。我可以做这样的事情:
但这是实现这一目标的正确方法吗?CPU太难了?
编辑:基本上我想要实现的是,即使有一个异步函数,也让代码看起来像是同步的。
EDIT2:由于上面编辑中提到的原因,我的问题不重复。
java - How to avoid busy spinning of a pause-able producer?
Background
I've found some GIF animation library, which has a background thread that constantly decodes the current frame into a bitmap, being a producer to other thread :
The sample POC I've made for this is available here.
The problem
This is inefficient, because when the thread gets to a point that mIsPlaying
is false, it just waits there and constantly checks it. In fact, it causes this thread to do more CPU usage somehow (I checked via the profiler).
In fact, it goes from 3-5% of CPU, to 12-14% CPU.
What I've tried
I had a good knowledge about threads in the past, and I know that simply putting a wait
and notify
is dangerous as it could still cause the thread to wait on some rare cases. For example when it identified that it should wait, and then before it starts to wait, the outside thread marks it that it shouldn't wait.
This behavior is called "busy spinning" or "Busy Waiting" , and there are actually some solutions about it, in the case of multiple threads that need to work together, here .
But here I think it's a bit different. The wait isn't for some thread to finish its work. It's for temporary waiting.
Another issue here is that the consumer thread is the UI thread, as it is the one that needs to get the bitmap and view it, so it can't just wait work like a consumer-producer solution (UI must never wait, as it can cause "jank") .
The question
What's the proper way to avoid spinning here?
multithreading - 使线程等待条件,但允许线程在等待或侦听信号时保持可用
给定线程 A 必须将工作分派给线程 B 的情况,是否有任何同步机制允许线程 A 不返回,但仍可用于其他任务,直到线程 B 完成,然后线程 A 可以返回?
这不是特定于语言的,但简单的 c 语言将是对此做出回应的绝佳选择。
这可能是绝对违反直觉的。听起来确实如此,但我必须在假设之前先问一下……
请注意 This is a made up hypothetical situation
,我感兴趣。我不是在寻找现有问题的解决方案,因此替代的并发解决方案完全没有意义。我没有它的代码,如果我在其中,我可以想到一些替代代码工程解决方案来避免这种设置。我只是想知道一个线程是否可以以某种方式使用,同时等待来自另一个线程的信号,以及为此使用什么同步机制。
更新
正如我上面提到的,我知道如何同步线程等。我只对我在这里介绍的情况感兴趣。互斥量、信号量和锁各种机制都会同步访问资源,同步事件顺序,同步各种并发问题,是的。但我对如何正确地做到这一点不感兴趣。我只是编造了这种情况,我想知道它是否可以通过前面描述的机制来解决。
更新 2
似乎我为那些认为自己是并发专家的人打开了一个门户,他们可以随机传送和讲授他们认为世界其他地方不知道线程是如何工作的。我只是问是否有这种情况的机制,而不是解决方案,不是“同步的正确方法”,也不是更好的方法。我已经知道我会做什么,而且永远不会陷入这种虚构的境地。这只是假设。
uart - 发送完成回调未在 UART DMA 中被调用
我想做的很简单。通过 DMA 传输并等待它被传输。然后接收并等待接收。
当我注释掉接收部分(包括回调)时,它将进入传输完成回调函数。但是当我取消注释接收部分时,它不会进入 tx cplt 回调,而是直接进入接收 cplt 回调。当我检查接收缓冲区时,我没有得到我所期望的(显然)。可能出了什么问题?
我正在使用 Atollic True Studio V 9.0、CubeMx v5.1.0、STM32F407VG-DISC1 板并为 UART2 启用了 DMA。
我尝试通过 UART DMA 发送字符缓冲区并接收它。它似乎根本没有传输,因为它没有进入 txCplt 回调。它直接进入 Rxcplt 回调。
我希望 rx_arr 会被 0,2,4,6,...18 填满,但它会被垃圾填满
c++ - 是否有标准功能忙于等待条件或直到超时
我需要在我的程序中等待子系统。在不同的地方一个必须等待不同的条件。我知道我也可以使用线程和条件变量。但是由于子系统(用 C 语言编程的裸机)是通过共享内存连接的,没有注册中断——一个线程无论如何都需要轮询。
所以我做了下面的模板能够等待任何事情。我想知道是否已经有一个可以用于此的 STL 函数?
2019-05-23:关于评论和答案的代码更新
python - 死锁主线程而不是忙着等待更好吗?
我有一个主线程,它启动多个监听文件系统事件的守护线程。我需要保持主线程活着。为此,我可以使用 while 循环,或“死锁”它。哪个更好,性能明智?
或者
在这两种情况下,我都可以使用 ctrl+c 中断主线程。
编辑:还是有更好的方法?