interlocked

What's the difference between InterlockedCompareExchange Release() and Acquire()?

淺唱寂寞╮ 提交于 2019-11-29 07:13:31
What's the difference between InterlockedCompareExchangeRelease() and InterlockedCompareExchangeAcquire() ? When I try to learn the synchronization functions with WIN32 API, I find there are two functions named differently but seems to do the same thing: LONG __cdecl InterlockedCompareExchangeRelease( __inout LONG volatile *Destination, __in LONG Exchange, __in LONG Comparand ); and LONG __cdecl InterlockedCompareExchangeAcquire( __inout LONG volatile *Destination, __in LONG Exchange, __in LONG Comparand ); I check the MSDN, it says those functions are: Performs an atomic compare-and-exchange

Memory barrier vs Interlocked impact on memory caches coherency timing

五迷三道 提交于 2019-11-29 07:03:12
问题 Simplified question: Is there a difference in timing of memory caches coherency (or "flushing") caused by Interlocked operations compared to Memory barriers? Let's consider in C# - any Interlocked operations vs Thread.MemoryBarrier(). I believe there is a difference. Background: I read quite few information about memory barriers - all the impact on prevention of specific types of memory interaction instructions reordering, but I couldn't find consistent info on whether they should cause

C# Interlocked Exchange

南笙酒味 提交于 2019-11-29 02:18:48
I have a bit of my game which looks like this: public static float Time; float someValue = 123; Interlocked.Exchange(ref Time, someValue); I want to change Time to be a Uint32; however, when I try to use UInt32 instead of float for the values, it protests that the type must be a reference type. Float is not a reference type, so I know it's technically possible to do this with non-reference types. Is there any practical way to make this work with UInt32 ? Although ugly, it is actually possible to perform an atomic Exchange or CompareExchange on an enum or other blittable value type of 64 bits

Performance of Interlocked.Increment

人盡茶涼 提交于 2019-11-29 01:11:15
Is Interlocked.Increment(ref x) faster or slower than x++ for ints and longs on various platforms? It is slower since it forces the action to occur atomically and it acts as a memory barrier, eliminating the processor's ability to re-order memory accesses around the instruction. You should be using Interlocked.Increment when you want the action to be atomic on state that can be shared between threads - it's not intended to be a full replacement for x++. In our experience the InterlockedIncrement() et al on Windows are quite significant impacts. In one sample case we were able to eliminate the

This is Thread-Safe right?

别来无恙 提交于 2019-11-28 21:03:21
Just checking... _count is being accessed safely, right? Both methods are accessed by multiple threads. private int _count; public void CheckForWork() { if (_count >= MAXIMUM) return; Interlocked.Increment(ref _count); Task t = Task.Run(() => Work()); t.ContinueWith(CompletedWorkHandler); } public void CompletedWorkHandler(Task completedTask) { Interlocked.Decrement(ref _count); // Handle errors, etc... } No, if (_count >= MAXIMUM) return; is not thread safe. edit: You'd have to lock around the read too, which should then logically be grouped with the increment, so I'd rewrite like private int

Is a lock (wait) free doubly linked list possible?

六眼飞鱼酱① 提交于 2019-11-28 19:51:09
Asking this question with C# tag, but if it is possible, it should be possible in any language. Is it possible to implement a doubly linked list using Interlocked operations to provide no-wait locking? I would want to insert, add and remove, and clear without waiting. Unknown A simple google search will reveal many lock-free doubly linked list papers. However, they are based on atomic CAS (compare and swap). I don't know how atomic the operations in C# are, but according to this website http://www.albahari.com/threading/part4.aspx C# operations are only guaranteed to be atomic for reading and

Performance of Interlocked.Increment

两盒软妹~` 提交于 2019-11-27 15:43:27
问题 Is Interlocked.Increment(ref x) faster or slower than x++ for ints and longs on various platforms? 回答1: It is slower since it forces the action to occur atomically and it acts as a memory barrier, eliminating the processor's ability to re-order memory accesses around the instruction. You should be using Interlocked.Increment when you want the action to be atomic on state that can be shared between threads - it's not intended to be a full replacement for x++. 回答2: In our experience the

This is Thread-Safe right?

坚强是说给别人听的谎言 提交于 2019-11-27 13:26:02
问题 Just checking... _count is being accessed safely, right? Both methods are accessed by multiple threads. private int _count; public void CheckForWork() { if (_count >= MAXIMUM) return; Interlocked.Increment(ref _count); Task t = Task.Run(() => Work()); t.ContinueWith(CompletedWorkHandler); } public void CompletedWorkHandler(Task completedTask) { Interlocked.Decrement(ref _count); // Handle errors, etc... } 回答1: No, if (_count >= MAXIMUM) return; is not thread safe. edit: You'd have to lock

Is a lock (wait) free doubly linked list possible?

落爺英雄遲暮 提交于 2019-11-27 12:32:35
问题 Asking this question with C# tag, but if it is possible, it should be possible in any language. Is it possible to implement a doubly linked list using Interlocked operations to provide no-wait locking? I would want to insert, add and remove, and clear without waiting. 回答1: A simple google search will reveal many lock-free doubly linked list papers. However, they are based on atomic CAS (compare and swap). I don't know how atomic the operations in C# are, but according to this website http:/

Reading an int that's updated by Interlocked on other threads

末鹿安然 提交于 2019-11-26 13:45:16
问题 (This is a repeat of: How to correctly read an Interlocked.Increment'ed int field? but, after reading the answers and comments, I'm still not sure of the right answer.) There's some code that I don't own and can't change to use locks that increments an int counter (numberOfUpdates) in several different threads. All calls use: Interlocked.Increment(ref numberOfUpdates); I want to read numberOfUpdates in my code. Now since this is an int, I know that it can't tear. But what's the best way to