Note that there are some explanatory texts on larger screens.

plurals
  1. POUse Interlocked.CompareExchange sync with a sync object in a Timer HandleElapsed handler
    text
    copied!<p>I m reading a MSDN example <a href="http://msdn.microsoft.com/en-us/library/system.timers.timer.stop.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.timers.timer.stop.aspx</a></p> <p>In the timer.stop example, i suspected its way of using Interlocked.CompareExchange is not right.</p> <pre><code>private static void HandleElapsed(object sender, ElapsedEventArgs e) { numEvents += 1; // This example assumes that overlapping events can be // discarded. That is, if an Elapsed event is raised before // the previous event is finished processing, the second // event is ignored. // // CompareExchange is used to take control of syncPoint, // and to determine whether the attempt was successful. // CompareExchange attempts to put 1 into syncPoint, but // only if the current value of syncPoint is zero // (specified by the third parameter). If another thread // has set syncPoint to 1, or if the control thread has // set syncPoint to -1, the current event is skipped. // (Normally it would not be necessary to use a local // variable for the return value. A local variable is // used here to determine the reason the event was // skipped.) // int sync = Interlocked.CompareExchange(ref syncPoint, 1, 0); if (sync == 0) { // No other event was executing. // The event handler simulates an amount of work // lasting between 50 and 200 milliseconds, so that // some events will overlap. int delay = timerIntervalBase - timerIntervalDelta / 2 + rand.Next(timerIntervalDelta); Thread.Sleep(delay); numExecuted += 1; // Release control of syncPoint. syncPoint = 0; } else { if (sync == 1) { numSkipped += 1; } else { numLate += 1; } } } </code></pre> <p>My question is this block</p> <pre><code>int sync = Interlocked.CompareExchange(ref syncPoint, 1, 0); if (sync == 0) { // lots of code here syncPoint = 0; } </code></pre> <p>Should it be like </p> <pre><code>int sync = Interlocked.CompareExchange(ref syncPoint, 1, 0); if (sync == 0) { // lots of code here Interlocked.CompareExchange(ref syncPoint, 0, 1) } </code></pre> <p>Because the original assigment <code>syncPoint = 0;</code> is not thread safe. Am I right?</p> <p><strong>UPDATE</strong>:</p> <p>I updated the example, i have no question about the return value of ComareExchange, my question is about the the assignment of variable syncpoint in the end of this if block. There is no interlock on it at all. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload