Я читаю пример MSDN http://msdn.microsoft.com/en-us/library/system.timers.timer.stop.aspx
В примере с timer.stop я подозревал, что его способ использования Interlocked.CompareExchange неверен.
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; }
}
}
Мой вопрос в этом блоке
int sync = Interlocked.CompareExchange(ref syncPoint, 1, 0);
if (sync == 0)
{
// lots of code here
syncPoint = 0;
}
Должно ли это быть как
int sync = Interlocked.CompareExchange(ref syncPoint, 1, 0);
if (sync == 0)
{
// lots of code here
Interlocked.CompareExchange(ref syncPoint, 0, 1)
}
Поскольку исходное назначение syncPoint = 0;
не является потокобезопасным. Я прав?
ОБНОВЛЕНИЕ:
Я обновил пример, у меня нет вопросов о возвращаемом значении ComareExchange, мой вопрос касается назначения переменной syncpoint в конце этого блока if. Блокировки на нем нет вообще.