5

Is calling Array.Resize on an array that is being used as a buffer for SAEA threadsafe? different threads all write to their own assigned part of the array, I just want to make the array bigger without locking once the initialized size runs out as connected users increase.

byte[] buffer; //Accessed
object expand_Lock = new object();

public void AsyncAccept()
{
    //Lock here so we don't resize the buffer twice at once
    lock(expand_Lock)
    {
        if (bufferFull)
        {
            Array.Resize(buffer, buffer.Length + 2048 * 100); //Add space for 100 more args
            //Is Array.Resize threadsafe if buffer can be read/wrote to at anytime?
            AssignMoreSAEA(2048, 100); //irrelevant to question what this does
        }
    }
}
2
  • 1
    From the documentation: This method allocates a new array with the specified size, copies elements from the old array to the new one, and then replaces the old array with the new one. array must be a one-dimensional array. Commented May 1, 2014 at 16:26
  • It actually is thread-safe, note that you didn't write the code correctly. The 1st argument requires ref and object reference updates are atomic. Whatever other thread is using the array will still have a valid reference to the old array object, it doesn't magically disappear or become invalid. This is a problem, your code will not crash. It will misbehave by the other thread reading stale data. Commented May 1, 2014 at 16:51

2 Answers 2

3

No, Array.Resize is not thread-safe. The array that's being resized may have to be moved around in memory and the resized array will not even be the same array as the one passed in.

This means that one thread may be writing into the original array while Array.Resize is creating the new array and copying over items - the copy operation is not protected. This can result in loss of data.

Sign up to request clarification or add additional context in comments.

5 Comments

According to the documentation it will not be the same array.
@BrianRasmussen Yes, exactly. I was looking at Array.Resize in ILSpy to see exactly what's going on.
@BrianRasmussen I fixed the wording of that sentence.
Totally forgot .Net mscorlib is opensourced now referencesource.microsoft.com/#mscorlib/system/… explains everything.
The location of both the source and resized arrays in memory is completely irrelevant to this discussion. It should be removed from your answer since it is misleading.
2
  • The method will not alter the original array.
  • The method will not throw an exception in multi-threaded scenarios that it would not throw in single-threaded scenarios, because the source array has a fixed size and the elements are not being dereferenced.
  • The method is thread-safe if the new length is equal to the old length, because according to the documentation the method does nothing in that case.
  • If the source array is modified during the operation, the contents of the resized array are unspecified. If the elements of the array fit into a machine word (IntPtr.Size or smaller, including reference types), the elements of the resulting array will each either contain the value of the same element in the source array when the method was invoked, or any value that was assigned to that element during the execution of the resize operation. If the elements do not fit into a machine word, then any element which is modified during the execution of the resize operation will result in an unspecified value for the corresponding element in the resized array.

2 Comments

You mean if OriginalArray.Length > int.MaxValue, itl lose data? Also whatever happened to the original array after Array.Copy(.. but before array = newArray; will be lost? (based on the referencesource.microsoft.com source of Array.Resize)
@VansS You can't use a specific implementation to explain the behavior here, because it's not guaranteed to be the same implementation used by every version of the framework. You have to explain the behavior in terms of the actual documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.