Questions tagged [interlocked-increment]

Interlocked.Increment increments a specified variable and stores the result, as an atomic operation.

Interlocked.Increment increments a specified variable and stores the result, as an atomic operation.

Example

private static int _variable = 0;

public void SafeIncrement()
{
    Interlocked.Increment(ref _variable);
}

References

34 questions
29
votes
7 answers

Interlocked.CompareExchange using GreaterThan or LessThan instead of equality

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…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
22
votes
6 answers

Performance of Interlocked.Increment

Is Interlocked.Increment(ref x) faster or slower than x++ for ints and longs on various platforms?
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
20
votes
6 answers

C# multi-threaded unsigned increment

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…
FlappySocks
  • 3,772
  • 3
  • 32
  • 33
11
votes
1 answer

Atomic increment of 64 bit variable on 32 bit environment

Writing an answer for another question some interesting things came out and now I can't understand how Interlocked.Increment(ref long value) works on 32 bit systems. Let me explain. Native InterlockedIncrement64 is now not available when compiling…
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
11
votes
3 answers

Does C# ++ operator become threadsafe in foreach loop?

Recently I moved from VB to C#, so I often use a C# to VB.NET converter to understand syntax differences. While moving next method to VB I noticed an interesting thing. C# original code: public bool ExceedsThreshold(int threshold, IEnumerable
AsValeO
  • 2,859
  • 3
  • 27
  • 64
8
votes
1 answer

Is it Safe to use 'Unsafe' Thread Functions?

Please pardon my slightly humorous title. I use two different definitions of the word 'safe' in it (obviously). I am rather new to threading (well, I have used threading for many years, but only very simple forms of it). Now I am faced with the…
5
votes
2 answers

Interlocked.Increment and return of incremented value

We have a method which maintains global sequence index of all events in our application. As it is website, it is expected to have such method thread safe. Thread-safe implementation was following: private static long lastUsedIndex = -1; public…
4
votes
3 answers

Difference between interlocked variable access AND critical sections interlocked increment

can someone help explain the different between interlocked variable access AND critical sections interlocked increment in c++? thanks, much appreciated, in advance.
Beef
  • 1,413
  • 6
  • 21
  • 36
4
votes
3 answers

confused on usage of Interlocked.Increment and Lock

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.…
user145610
  • 2,949
  • 4
  • 43
  • 75
3
votes
5 answers

Can I use interlocked operations to update multiple values to avoid locking a critical section/mutex?

I have a multithreaded application (C++) where I need to increment/change a series of values. If I use a series of Interlocked operations, are they considered to be a single atomic operation ? Like in this…
Gratian Lup
  • 1,485
  • 3
  • 19
  • 29
3
votes
1 answer

C# - Interlocked Increment on a Dictionary Like List

I know that the int wont have a fixed position in memory so it simply cant work like that. But The exact same portion of code will be run concurrently with different names, parameters e.t.c I need to essentially pass a string of "Name" and then…
user8549339
3
votes
1 answer

Manually writing a multithreaded loop - suboptimal scalability

I have written this test application: it goes through iterations from 0 to 9999, for each integer in the range it calculates some useless but calculation-intensive function. As a result the program outputs the sum of function values. To make it run…
2
votes
3 answers

Can a C# blocking FIFO queue leak messages?

I'm working on an academic open source project and now I need to create a fast blocking FIFO queue in C#. My first implementation simply wrapped a synchronized queue (w/dynamic expansion) within a reader's semaphore, then I decided to re-implement…
usr-local-ΕΨΗΕΛΩΝ
  • 26,101
  • 30
  • 154
  • 305
2
votes
1 answer

Using InterlockedIncrement to increment STL map's value field. Is it threadsafe?

I have a map defined like this. typedef std::map< tstring, unsigned int > ErrorToCount_T; ErrorToCount_T m_ErrorToSuppress; I am using like it like this. ErrorToCount_T::iterator itr = m_ErrorToSuppress.find( val ); if( itr !=…
2
votes
2 answers

How do I check for overflow after an Interlocked.Increment in C#?

What is the correct way to check for an overflow after a call to Interlocked.Increment? I have an ID generator that generates unique ids during the program's execution, and currently I test if the increment returned zero. public static class…
Suzanne Soy
  • 3,027
  • 6
  • 38
  • 56
1
2 3