Questions tagged [interlocked]

Provides atomic operations for variables that are shared by multiple threads.

Provides atomic operations for variables that are shared by multiple threads.

References

233 questions
1
vote
1 answer

Strange behaviour of a System.Threading.Timer

I am trying to get some insight into how Timer will use thread pool. I wrote following simple snippet. class Program { static private readonly Action Action = () => { …
Klark
  • 8,162
  • 3
  • 37
  • 61
1
vote
2 answers

ConcurrentQueue one element is taken by two threads

I want two threads to be working with one queue. First thread should be called every 2 seconds, and the second thread - every 3 seconds. Both threads should start at the same time. I have a problem when accessing first element of the queue. Both…
Illania
  • 117
  • 1
  • 1
  • 14
1
vote
1 answer

Use Interlocked.CompareExchange sync with a sync object in a Timer HandleElapsed handler

I m reading a MSDN example http://msdn.microsoft.com/en-us/library/system.timers.timer.stop.aspx In the timer.stop example, i suspected its way of using Interlocked.CompareExchange is not right. private static void HandleElapsed(object sender,…
ValidfroM
  • 2,626
  • 3
  • 30
  • 41
1
vote
0 answers

Space and Time Consumption of (Custom) Performance Counters

In terms of the speed of incrementing a (custom) performance counter, I understand performance counters to be lock-free, processor primitives. I suspect this means they can execute in the space of a few dozen CPU cycles - which means they are so…
Brent Arias
  • 29,277
  • 40
  • 133
  • 234
1
vote
2 answers

InterlockedExchange on two CPU cores

I have a Windows 7 driver where I want to synchronize access to a variable. Can I use InterlockedExchange for it? My current understanding of InterlockedExchange is, that InterlockedExchange is done via compiler intrinsics. That means, the read…
Christian Troester
  • 167
  • 1
  • 2
  • 10
1
vote
3 answers

Safety of Interlock.Exchange and Garbage Collection

I have an object that I am accessing from two threads. One thread calls a long-running member function on the object that returns a value. The second thread updates the object used to produce that value. I if I call Interlock.Exchange to replace…
OldTimer
  • 71
  • 1
  • 5
1
vote
1 answer

Can Interlocked be used in a Finalizer?

Suppose I need to clean up some managed resources in a finalizer, or at least record it somewhere in a thread safe way that a clean up is needed. From what I understand, taking locks in a finalizer is strictly verboten but how about the Interlocked…
user1096188
  • 1,809
  • 12
  • 11
1
vote
2 answers

How to interlock divs?

I would like to interlock an undefined number of divs (as in Pinterest) from this: ++++++++ ******** + + * * + + * * + + ******** + + ++++++++ ~~~~~~~~ -------- ~ ~ - - ~ ~ - - ~~~~~~~~ - …
Davide
  • 1,635
  • 1
  • 16
  • 29
1
vote
1 answer

Will passing a value type variable by reference into a static method thread safe?

the code looks like this: public class JobManager { public static void TrackExceptionCount(ref int exceptionCount) { Interlocked.Increment(ref exceptionCount); } //other helper methods } I will call this method in other…
Jeff Chen
  • 736
  • 1
  • 8
  • 20
1
vote
1 answer

Using of Interlocked.CompareExchange

guys, I would like to you evaluate next code below. As you see, I use Interlocked.CompareExchange, does it make sense in this context? (I am not sure, that it is correct). I'll be glad any notes, comments, etc. private static T GetItem(string…
Maxim Polishchuk
  • 334
  • 6
  • 17
1
vote
0 answers

Reverse-engineering frequent interlockedIncrement/Decrement

I'm trying to reverse engineer some code and run into a common pattern many times, but I'm not sure what could it be. The code was compiled with VC++ 2010 and uses some external CLR components for display (but not for any internals as far as I can…
viraptor
  • 33,322
  • 10
  • 107
  • 191
0
votes
1 answer

interlocked operation on unanligned data

The win32 interlocked functions provide a mecanism for atomic operation on data. They are supposed to be thread-safe and multiprocessor-safe. What happen if the data is not aligned? the interlocked operations are still atomic? Ex.: incrementing a…
NorthWind
0
votes
2 answers

Why doesn't InterlockedCompareExchange return changed value?

LONG __cdecl InterlockedCompareExchange( __inout LONG volatile *Destination, __in LONG Exchange, __in LONG Comparand ); Return value The function returns the initial value of the Destination parameter. Just curious. Why does…
Benjamin
  • 10,085
  • 19
  • 80
  • 130
0
votes
1 answer

Is Interlock.CompareExchange atomic inside an if statement?

I am assuming that the piece of code is threadsafe, if ((Interlocked.CompareExchange(ref semaphore, 1, 0) == 0)) { //Do Stuff semaphore = 0; } However I am wondering why: In my mind I see this as two operations, First it compares the semaphore…
0
votes
0 answers

Thread safe mechanism to swap immutable references

Let's say I have a complex object that is immutable that stores a bunch of data. Let's call this type MyDataCache. Now, let's say I have a MyDataCacheManager which holds a reference to a MyDataCache. Callers can ask the manager for a reference of…
millie
  • 2,642
  • 10
  • 39
  • 58