interlocked

Shared enum between multiple threads

佐手、 提交于 2019-12-12 04:49:22
问题 I have an enumeration that is shared between multiple threads: public enum Action { Read, Write, None } Within a class I have a variable of Action type: public Action _action; This is a shared variable, that is, it is updated and read from multiple threads. For example, from one thread I do: _action = Action.Read And from another one: if (_action == Action.Read) { } else if (_action == Action.Write) { } else if (_Action == Action.None) { } else { } So I would like to use Interlock to update

Fastest way to safely read contents of long[] whose elements are changed concurrently

為{幸葍}努か 提交于 2019-12-10 17:44:35
问题 When you have a long[] myArray = new long[256]; whose items are changed by multiple threads using Interlocked.Increment(ref myArray[x]) it sure won't be possible to get a snapshot of myArray at one point in time, as there are non-locked writes going on concurrently, so I'm not trying to get that. So do I really have to Volatile.Read of every single element like this to get a copy of all values at some point in the past? long[] copy = new long[256]; for (int i = 0; i < 256; i++) copy[i] =

Is Interlocked.CompareExchange really faster than a simple lock?

假如想象 提交于 2019-12-10 13:38:32
问题 I came across a ConcurrentDictionary implementation for .NET 3.5 (I'm so sorry I could find the link right now) that uses this approach for locking: var current = Thread.CurrentThread.ManagedThreadId; while (Interlocked.CompareExchange(ref owner, current, 0) != current) { } // PROCESS SOMETHING HERE if (current != Interlocked.Exchange(ref owner, 0)) throw new UnauthorizedAccessException("Thread had access to cache even though it shouldn't have."); Instead of the traditional lock : lock

Correct way of checking when ThreadPool threads are done?

跟風遠走 提交于 2019-12-10 12:17:19
问题 I'm looking for a way of checking when all threads in threadpool have finished their tasks. Currently I'm using a counter that decrements when a thread has finished it's job and when counter == 0 I'm invoking my WorkComplete method. This seems to work but when i get to the final 'job' it doesn't appear to process the result? Or at least the UI doesn't get it. Here is what i currently have: Queuing work items + incrementing counter foreach (string s in URLs) { ThreadPool.QueueUserWorkItem(new

Updating property of a singleton with Thread Safety

旧街凉风 提交于 2019-12-08 03:47:32
问题 Our setup is: Asp.NET + MVC5 using AutoFac for DI. We have a class (which is a singleton) which is managing the access tokens for a variety of services. Every now and then, these tokens get too close to expiry (less then 10 minutes) and we request new tokens, refresh them. My current implementation looks like this: // member int used for interlocking int m_inter = 0; private string Token { get; set; } private DateTimeOffset TokenExpiry { get; set; } public SingletonClassConstructor() { //

Updating property of a singleton with Thread Safety

依然范特西╮ 提交于 2019-12-06 15:49:18
Our setup is: Asp.NET + MVC5 using AutoFac for DI. We have a class (which is a singleton) which is managing the access tokens for a variety of services. Every now and then, these tokens get too close to expiry (less then 10 minutes) and we request new tokens, refresh them. My current implementation looks like this: // member int used for interlocking int m_inter = 0; private string Token { get; set; } private DateTimeOffset TokenExpiry { get; set; } public SingletonClassConstructor() { // Make sure the Token has some value. RefreshToken(); } public string GetCredentials() { if ((TokenExpiry -

C# Interlocked functions as a lock mechanism?

Deadly 提交于 2019-12-06 05:09:47
While I was reading about ReaderWriterLockSlim lock mechanism , There was this guy who suggested that Interlock Functions can be used for a finer locking Also, I found here another answer from Marc : ...Writes make their change to a cloned copy, then use Interlocked.CompareExchange to swap the reference (re-applying their change if another thread mutated the reference in the interim). Well , Currently all I know about Interlocked object , is that it is used (in a multithreaded environment) to do atomic additions , compare , compareExchange operations. (and I know how to use it) But ( and here

long vs {0L}[0]

风格不统一 提交于 2019-12-06 04:28:52
In one of our old services I found such piece of code (comments are original): long[] tasksCounter = {0}; //boxing for long counters long[] errorsCounter = {0}; //boxing for long counters Further in the code these "arrays" are used with Interlocked class: Interlocked.Increment(ref errorsCounter[0]) , Interlocked.Read(ref errorsCounter[0]) etc). I wonder why did not the author use basicaly long tasksCounter, errorsCounter ? Probably this approach has benefits I don't know about? It's probably worth mentioning that the variables are used in async lambda. When I change it to basic long Resharper

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

你离开我真会死。 提交于 2019-12-05 18:28:49
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 void EnterSpin() { int num = 0; while (this.lockHeld != null || Interlocked.CompareExchange(ref this

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

情到浓时终转凉″ 提交于 2019-12-04 22:06:46
问题 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.