3

Looking through the sources of .NET Core, I've found the following piece in ConcurrentQueue<T> source:

//We need do Interlocked.Increment and value/state update in a finally block to ensure that they run
//without interuption. This is to prevent anything from happening between them, and another dequeue
//thread maybe spinning forever to wait for _state[] to be true;
try
{ }
finally
{
    newhigh = Interlocked.Increment(ref _high);
    if (newhigh <= SEGMENT_SIZE - 1)
    {
        _array[newhigh] = value;
        _state[newhigh]._value = true;
    }

    //if this thread takes up the last slot in the segment, then this thread is responsible
    //to grow a new segment. Calling Grow must be in the finally block too for reliability reason:
    //if thread abort during Grow, other threads will be left busy spinning forever.
    if (newhigh == SEGMENT_SIZE - 1)
    {
        Grow();
    }
}

So the question is what the purpose is of an empty try block here and putting that code into a finally block?

DixonD
  • 6,557
  • 5
  • 31
  • 52
  • That comment reads: `We need do Interlocked.Increment and value/state update in a finally block to ensure that they run without interuption. This is to prevent anything from happening between them, and another dequeue thread maybe spinning forever to wait for _state[] to be true`. – GSerg Sep 10 '15 at 08:19

0 Answers0