interlocked-increment

confused on usage of Interlocked.Increment and Lock

旧巷老猫 提交于 2019-12-11 00:48:56
问题 I understand the functionality of Interlocked.Increment and lock() . But I'm confused on when to use one or the other. As far as I can tell Interlocked.Increment increments shared int/long value, whereas as lock() is meant to lock region of code. For example, if i want to update string value it is possible with lock() : lock(_object) { sharedString = "Hi"; } However, this is not possible with Interlocked class. Why can't this be done via Interlocked ? What's the difference between these

C# multi-threaded unsigned increment

寵の児 提交于 2019-12-09 02:37:01
问题 I want to increment an unsigned integer from multiple threads. I know about Interlocked.Increment, but it does not handle unsigned integers. I could use lock(), but I would rather not if possible for performance reasons. Is it thread safe just to increment it in the normal way? It would not matter if the occasional increment got lost, as it's only used for statistics. What I don't want is the value to get corrupted. 回答1: You say you don't want to use lock for performance reasons - but have

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

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

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

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

Reading an int that&#39;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