interlocked

Using Interlocked.CompareExchange with a class

半世苍凉 提交于 2021-02-16 04:32:50
问题 System.Threading.Interlocked.CompareExchange operator provides atomic (thus thread-safe) C# implementation of the Compare-And-Swap operation. For example int i = 5; Interlocked.CompareExchange(ref i, 10, 5); After this command, the int i would have a value = 10. And also the compare and exchange happens atomically (single operation). When I tried using this with a class instance, the compare fails and the values are not exchanged. public class X { public int y; public X(int val) { y = val; }

Using Interlocked.CompareExchange with a class

∥☆過路亽.° 提交于 2021-02-16 04:32:44
问题 System.Threading.Interlocked.CompareExchange operator provides atomic (thus thread-safe) C# implementation of the Compare-And-Swap operation. For example int i = 5; Interlocked.CompareExchange(ref i, 10, 5); After this command, the int i would have a value = 10. And also the compare and exchange happens atomically (single operation). When I tried using this with a class instance, the compare fails and the values are not exchanged. public class X { public int y; public X(int val) { y = val; }

Lockfree Read value after Interlocked.Exchange?

喜你入骨 提交于 2021-02-08 06:49:55
问题 Lets say we have a class like so: public class Foo { private Bar bar = new Bar(); public void DoStuffInThread1() { var old = Interlocked.Exchange(ref bar,new Bar()); //do things with old //everything is fine here, I'm sure I have the previous bar value } public void OtherStuffFromThread2() { //how do I ensure that I have the latest bar ref here //considering mem cahces etc bar.Something(); } } And lets say we have two threads, one operating on DoStuffInThread1 and another on

Lockfree Read value after Interlocked.Exchange?

若如初见. 提交于 2021-02-08 06:48:53
问题 Lets say we have a class like so: public class Foo { private Bar bar = new Bar(); public void DoStuffInThread1() { var old = Interlocked.Exchange(ref bar,new Bar()); //do things with old //everything is fine here, I'm sure I have the previous bar value } public void OtherStuffFromThread2() { //how do I ensure that I have the latest bar ref here //considering mem cahces etc bar.Something(); } } And lets say we have two threads, one operating on DoStuffInThread1 and another on

How should I increment a number for a round robin threading scenario with least contention?

此生再无相见时 提交于 2021-02-07 19:19:43
问题 If many threads are calling GetNextNumber simultaneously with the following code, GetNextNumber will return 1 more times than any other numbers. private class RoundRobbinNumber { private int _maxNumbers = 10; private int _lastNumber; private RoundRobbinNumber(int maxNumbers) { _maxNumbers = maxNumbers; } public int GetNextNumber() { int nextNumber = Interlocked.Increment(ref _lastNumber); if (_lastNumber > _maxNumbers) { Interlocked.CompareExchange(ref _lastNumber, 1, _maxNumbers); nextNumber

How should I increment a number for a round robin threading scenario with least contention?

随声附和 提交于 2021-02-07 19:19:16
问题 If many threads are calling GetNextNumber simultaneously with the following code, GetNextNumber will return 1 more times than any other numbers. private class RoundRobbinNumber { private int _maxNumbers = 10; private int _lastNumber; private RoundRobbinNumber(int maxNumbers) { _maxNumbers = maxNumbers; } public int GetNextNumber() { int nextNumber = Interlocked.Increment(ref _lastNumber); if (_lastNumber > _maxNumbers) { Interlocked.CompareExchange(ref _lastNumber, 1, _maxNumbers); nextNumber

When should & shouldn't I use this C# utility class to control threads via Interlocked

妖精的绣舞 提交于 2020-01-13 16:29:10
问题 I'm trying to understand the logic behind how this class was written, and when I should and shouldn't use it. Any insight would be appreciated internal struct SpinLock { private volatile int lockHeld; private readonly static int processorCount; public bool IsHeld { get { return this.lockHeld != 0; } } static SpinLock() { SpinLock.processorCount = Environment.ProcessorCount; } public void Enter() { if (Interlocked.CompareExchange(ref this.lockHeld, 1, 0) != 0) { this.EnterSpin(); } } private

Atomic unlocked access to 64bit blocks of Memory Mapped Files in .NET

我是研究僧i 提交于 2020-01-06 03:52:09
问题 We need to share very efficiently block of constantly changing information between two processes. Information fits in 64bits block of memory - so inside one process we'd be able to use Interlocked operations (or probably even just ordinary reads/writes) to ensure lock-free access to correct state of information (not just partially written). How can we write and ready block of 64bit data into MMF without locking and synchronization in order to make sure that we don't read partially written

very strange and severe multithread inconsistency problem c#

一曲冷凌霜 提交于 2020-01-05 03:08:29
问题 I have a very simple watchdog program with 2 threads. One thread is updating a long variable and the other thread reads the variable. and alert if it was more than X seconds from the last update. The problem is that sometimes (happens once a day more or less) the second thread reads a stale value of the variable. Sometimes it is stale value from 3 seconds ago (i.e. the first thread updated the long variable but after 3 seconds the other thread didn't get the new value) I am using lock, in