4

I was reading the source codes for ConcurrentQueue when I saw this (line 790-811):

//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 m_state[] to be true;
try
{ }
finally
{
    newhigh = Interlocked.Increment(ref m_high);
    if (newhigh <= SEGMENT_SIZE - 1)
    {
        m_array[newhigh] = value;
        m_state[newhigh].m_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();
    }
}

After reading that comment, I still have no idea what it means. What magic does finally block has that the official API would wrap a block of codes into a finally block which has an empty try block? Is there a simple example that would explain this special function of finally block?

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Jai
  • 8,165
  • 2
  • 21
  • 52

1 Answers1

4

Thread aborts cannot happen while a finally block is running. You are guaranteed execution by the CLR.

The code you posted is utilizing that fact to ensure cleanup always executes.

Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32
  • 1
    More accurately, you are guaranteed the code runs assuming the rest of the domain/process doesn't go away (i.e. somebody pulls the plug) and the thread isn't suspended. – Luaan Nov 12 '19 at 07:18