interlocked

Is there any advantage of using volatile keyword in contrast to use the Interlocked class?

て烟熏妆下的殇ゞ 提交于 2019-12-20 10:44:44
问题 In other words, can I do something with a volatile variable that could not also be solved with a normal variable and the Interlocked class? 回答1: EDIT: question largely rewritten To answer this question, I dived a bit further in the matter and found out a few things about volatile and Interlocked that I wasn't aware of. Let's clear that out, not only for me, but for this discussion and other people reading up on this: volatile read/write are supposed to be immune to reordering. This only means

Interlocked.CompareExchange with enum

為{幸葍}努か 提交于 2019-12-18 12:53:01
问题 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

Reading interlocked variables

岁酱吖の 提交于 2019-12-18 10:34:58
问题 Assume: A. C++ under WIN32. B. A properly aligned volatile integer incremented and decremented using InterlockedIncrement() and InterlockedDecrement() . __declspec (align(8)) volatile LONG _ServerState = 0; If I want to simply read _ServerState, do I need to read the variable via an InterlockedXXX function? For instance, I have seen code such as: LONG x = InterlockedExchange(&_ServerState, _ServerState); and LONG x = InterlockedCompareExchange(&_ServerState, _ServerState, _ServerState); The

Parallel.For() with Interlocked.CompareExchange(): poorer performance and slightly different results to serial version

懵懂的女人 提交于 2019-12-18 09:39:10
问题 I experimented with calculating the mean of a list using Parallel.For() . I decided against it as it is about four times slower than a simple serial version. Yet I am intrigued by the fact that it does not yield exactly the same result as the serial one and I thought it would be instructive to learn why. My code is: public static double Mean(this IList<double> list) { double sum = 0.0; Parallel.For(0, list.Count, i => { double initialSum; double incrementedSum; SpinWait spinWait = new

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

谁说我不能喝 提交于 2019-12-18 05:02:38
问题 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

InterlockedExchange and memory alignment

守給你的承諾、 提交于 2019-12-17 18:42:02
问题 I am confused that Microsoft says memory alignment is required for InterlockedExchange however, Intel documentation says that memory alignment is not required for LOCK. Am i missing something, or whatever? thanks from Microsoft MSDN Library Platform SDK: DLLs, Processes, and Threads InterlockedExchange The variable pointed to by the Target parameter must be aligned on a 32-bit boundary ; otherwise, this function will behave unpredictably on multiprocessor x86 systems and any non-x86 systems.

Alignment requirements for atomic x86 instructions vs. MS's InterlockedCompareExchange documentation?

守給你的承諾、 提交于 2019-12-17 05:53:19
问题 Microsoft offers the InterlockedCompareExchange function for performing atomic compare-and-swap operations. There is also an _InterlockedCompareExchange intrinsic . On x86 these are implemented using the lock cmpxchg instruction. However, reading through the documentation on these three approaches, they don't seem to agree on the alignment requirements. Intel's reference manual says nothing about alignment (other than that if alignment checking is enabled and an unaligned memory reference is

Waiting on Interlocked == 0?

前提是你 提交于 2019-12-14 04:13:00
问题 Disclaimer: My C# isn't even close to as good as my C++ I am trying to learn how to do async sockets in C# in order to write a test app for a component of mine. My former attempts using TcpClient ended in failure and you can read the outstanding questions on that here: TcpClient.NetworkStream Async operations - Canceling / Disconnect Detect errors with NetworkStream.WriteAsync Since, I could not get that working, I tried using Socket.BeginX and Socket.EndX instead. I got much further along.

Will the threadpool queue a timer's callback function, sometimes scheduling more than one thread at the same time?

孤街浪徒 提交于 2019-12-12 18:50:26
问题 In the following code TimerRecalcStatisticsElapsed should only have one instance of it running. The worker methods that this callback invokes is made to run in sequence, with a maximum of one thread running at a time. Question Part 1: If the timer's callback runs an a threadpool thread (as opposed to running the callback on a separate thread), is it correct to say the the threadpool might queue and defer the thread for later execution based on conditions (MaxThreads reached, threadpool

lock vs Interlocked.Exchange

邮差的信 提交于 2019-12-12 18:36:04
问题 I have an application which constantly (+-100ms) reads orders from a PLC and then puts them in a model which then gets read by multiple clients. For this im using the lock statement. Order Reading thread : lock (model) { //update object } Clients Reading : lock (model) { //serialize object to json string } send over tcp stream to client. But i could also use for the update : Interlocked.ExChange(oldObj, newObj) I don't want my clients to have to wait for a lock that is happening in the Order