Просматривая исходники .NET Core, я нашел следующий фрагмент в 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();
}
}
Итак, вопрос в том, какова цель здесь пустого try
блока и помещения этого кода в finally
блок?
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
. - person GSerg   schedule 10.09.2015