interlocked

Does Interlocked guarantee visibility to other threads in C# or do I still have to use volatile?

怎甘沉沦 提交于 2019-12-03 14:21:49
I've been reading the answer to a similar question , but I'm still a little confused... Abel had a great answer, but this is the part that I'm unsure about: ...declaring a variable volatile makes it volatile for every single access. It is impossible to force this behavior any other way, hence volatile cannot be replaced with Interlocked. This is needed in scenarios where other libraries, interfaces or hardware can access your variable and update it anytime, or need the most recent version. Does Interlocked guarantee visibility of the atomic operation to all threads, or do I still have to use

What is Interlocked.Increment actually doing?

我的未来我决定 提交于 2019-11-30 11:30:37
Interlocked.Increment seems like among the most standard/simple of operations one would need to perform in multithreaded code. I assume that the functionality of the method is some sort pattern that anyone with threading experience would be able to replicate. So basically what I am wondering is if someone could provide an exact duplicate (with explanation of how it works) of what the Interlocked.Increment method is actually doing internally? (I have looked for the source of the actual method but been unable to find it) According to Mr Albahari it does two things: makes the atomicity of the

Interlocked.CompareExchange<Int> using GreaterThan or LessThan instead of equality

允我心安 提交于 2019-11-30 11:26:55
问题 The System.Threading.Interlocked object allows for Addition (subtraction) and comparison as an atomic operation. It seems that a CompareExchange that just doesn't do equality but also GreaterThan/LessThan as an atomic comparison would be quite valuable. Would a hypothetical Interlocked.GreaterThan a feature of the IL or is it a CPU-level feature? Both? Lacking any other option, is it possible to create such a feature in C++ or direct IL code and expose that functionality to C#? 回答1: Update to

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

Interlocked.CompareExchange with enum

走远了吗. 提交于 2019-11-30 08:20:26
I'm trying to use Interlocked.CompareExchange with this enum: public enum State { Idle, Running, //... } The following code doesn't compile, but that's what I want do do: if (Interlocked.CompareExchange(ref state, State.Running, State.Idle) != State.Idle) { throw new InvalidOperationException("Unable to run - not idle"); } Sure I can use a int instead of the enum and use a property: private int state = (int)State.Idle; public State { get { return (State)state; } } Then cast the enums to a int: if (Interlocked.CompareExchange(ref state, (int)State.Running, (int)State.Idle) != (int)State.Idle) {

How to use interlocked operations against memory-mapped files in .Net

可紊 提交于 2019-11-30 07:16:59
Is there any way to use the Interlocked.CompareExchange(); and Interlocked.Increment(); methods against values stored in a memory-mapped file? I'd like to implement a multi-threaded service that will store its data in a memory-mapped file, but since it's multi-threaded I need to prevent conflicting writes, therefore I wonder about the Interlocked operations rather than using explicit locks. I know it's possible with native code, but can it be done in managed code on .NET 4.0? OK, this is how you do it! We had to figure this out, and I figured we could give some back to stackoverflow! class

Memory barrier vs Interlocked impact on memory caches coherency timing

萝らか妹 提交于 2019-11-30 06:48:50
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 immediate flushing of read/write queues. I actually found few sources mentioning that there is NO guarantee

Interlocked.CompareExchange<Int> using GreaterThan or LessThan instead of equality

为君一笑 提交于 2019-11-30 03:45:51
The System.Threading.Interlocked object allows for Addition (subtraction) and comparison as an atomic operation. It seems that a CompareExchange that just doesn't do equality but also GreaterThan/LessThan as an atomic comparison would be quite valuable. Would a hypothetical Interlocked.GreaterThan a feature of the IL or is it a CPU-level feature? Both? Lacking any other option, is it possible to create such a feature in C++ or direct IL code and expose that functionality to C#? Update to the later post I made here: we found a better way to make the greater comparison by using additional lock

Why everyone states that SpinLock is faster? [closed]

非 Y 不嫁゛ 提交于 2019-11-29 14:19:45
问题 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;

How to use interlocked operations against memory-mapped files in .Net

老子叫甜甜 提交于 2019-11-29 09:23:43
问题 Is there any way to use the Interlocked.CompareExchange(); and Interlocked.Increment(); methods against values stored in a memory-mapped file? I'd like to implement a multi-threaded service that will store its data in a memory-mapped file, but since it's multi-threaded I need to prevent conflicting writes, therefore I wonder about the Interlocked operations rather than using explicit locks. I know it's possible with native code, but can it be done in managed code on .NET 4.0? 回答1: OK, this is