spinlock

Why is “sleeping” not allowed while holding a spinlock? [duplicate]

廉价感情. 提交于 2019-12-03 09:57:16
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Why can't you sleep while holding spinlock? As far as I know, spinlocks should be used in short duration, and are only choices in code such as interrupt handler where sleeping (preemption) is not allowed. However, I do not know why there is such a "rule" that there SHOULD BE no sleeping at all while holding a spinlock. I know that it is not a recommended practice (since it is detrimental in performance), but I

Alternative to spinlock

烈酒焚心 提交于 2019-12-03 09:10:39
I am using the following spinlock approach: while(!hasPerformedAction()){ //wait for the user to perform the action //can add timer here too } setHasPerformedAction(false); return getActionPerfomed(); this basically waits for a user to perform an action and then returns it. Currently something requests an answer from the user before continuing, this is why I wait until input is received. However I was wondering if this is inefficient and if we are waiting for a while (i.e. <= 30 secs) will it slow down the pc that is running this app. Are there any other alternatives using this approach i.e.

Broken GLSL Spinlock/GLSL Locks Compendium

谁都会走 提交于 2019-12-01 20:00:41
问题 I have a setup where I need to lock, read some data, process, write some data, and then unlock. To this end, I made a locking texture as a layout(r32ui) coherent uniform uimage2D . The critical section's data is declared similarly. Unfortunately, all my attempts at a spinlock don't prevent race conditions, leading to incorrect results. I tried several different approaches. I thought I'd collect all the information I could find on GLSL locking, along with my results (GTX 580M). I have added a

Cross-platform and cross-process atomic int writes on file

别来无恙 提交于 2019-12-01 01:03:17
I'm writing an application that will have to be able to handle many concurrent accesses to it, either by threads as by processes. So no mutex'es or locks should be applied to this. To make the use of locks go down to a minimum, I'm designing for the file to be "append-only", so all data is first appended to disk, and then the address pointing to the info it has updated, is changed to refer to the new one. So I will need to implement a small lock system only to change this one int so it refers to the new address. How is the best way to do it? I was thinking about maybe putting a flag before the

Spinlock vs std::mutex::try_lock

余生颓废 提交于 2019-11-30 17:29:46
问题 What are the benefits of using a specifically designed spinlock (e.g. http://anki3d.org/spinlock) vs. code like this: std::mutex m; while (!m.try_lock()) {} # do work m.unlock(); 回答1: On typical hardware, there are massive benefits: Your naive "fake spinlock" may saturate internal CPU buses while the CPU spins, starving other physical cores including the physical core that holds the lock. If the CPU supports hyper-threading or something similar, your naive "fake spinlock" may consume

Why disabling interrupts disables kernel preemption and how spin lock disables preemption

放肆的年华 提交于 2019-11-30 11:31:11
问题 I am reading Linux Kernel Development recently, and I have a few questions related to disabling preemption. In the "Interrupt Control" section of chapter 7, it says: Moreover, disabling interrupts also disables kernel preemption. I also read from the book that kernel preemption can occur in the follow cases: When an interrupt handler exits, before returning to kernel-space. When kernel code becomes preemptible again. If a task in the kernel explicitly calls schedule() If a task in ther kernel

Why everyone states that SpinLock is faster? [closed]

拈花ヽ惹草 提交于 2019-11-30 09:26:31
I have read a lot of docs and articles and posts all over the internet. Almost everyone and everywhere commits that SpinLock is faster for a short running pieces of code, but I made a test, and it appears to me that simple Monitor.Enter works faster than SpinLock.Enter (Test is compiled against .NET 4.5) using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Threading.Tasks; using System.Linq; using System.Globalization; using System.ComponentModel; using System.Threading; using System.Net.Sockets; using System.Net; class

Intel Inspector reports a data race in my spinlock implementation

蓝咒 提交于 2019-11-30 09:16:54
I made a very simple spinlock using the Interlocked functions in Windows and tested it on a dual-core CPU (two threads that increment a variable); The program seems to work OK (it gives the same result every time, which is not the case when no synchronization is used), but Intel Parallel Inspector says that there is a race condition at value += j (see the code below). The warning disappears when using Critical Sections instead of my SpinLock. Is my implementation of SpinLock correct or not ? It's really strange, because all the used operations are atomic and have the proper memory barriers and

Difference between Mutex, Semaphore & Spin Locks

烈酒焚心 提交于 2019-11-30 06:11:56
问题 I am doing experiments with IPC, especially with Mutex, Semaphore and Spin Lock. What I learnt is Mutex is used for Asynchronous Locking (with sleeping (as per theories I read on NET)) Mechanism, Semaphore are Synchronous Locking (with Signaling and Sleeping) Mechanism, and Spin Locks are Synchronous but Non-sleeping Mechanism. Can anyone help me to clarify these stuff deeply? And another doubt is about Mutex, when I wrote program with thread & mutex, while one thread is running another

Why disabling interrupts disables kernel preemption and how spin lock disables preemption

删除回忆录丶 提交于 2019-11-29 23:19:00
I am reading Linux Kernel Development recently, and I have a few questions related to disabling preemption. In the "Interrupt Control" section of chapter 7, it says: Moreover, disabling interrupts also disables kernel preemption. I also read from the book that kernel preemption can occur in the follow cases: When an interrupt handler exits, before returning to kernel-space. When kernel code becomes preemptible again. If a task in the kernel explicitly calls schedule() If a task in ther kernel blocks (which results in a call to schedule()) But I can't relate disabling interrupts with these