As frequently stated, concurrency is hard. Ensuring that all of your data is accessed in a threadsafe manner is a real challenge. So when Ryan saw a method called ThreadSafeArray, it seemed like an obvious utility method: wrap an array up in a thread safe accessor, right?

Well, the signature of the function made Ryan suspicious. And the code…

public object[] ThreadSafeArray(System.Collections.ArrayList list)
{
	return list.ToArray();
}

There is nothing thread safe about this operation. But I suspect that's not what the method is trying to do. What it's trying to do is make a copy of the list. If you have a copy, your operations are automatically thread safe- mutations to the base list won't impact the copy.

It'll behave oddly if the list gets mutated during invocation, the copy is a shallow copy (which may not be what you want for many objects), obviously the resulting copy has no thread-safety protections if accessed from other threads, and making a whole copy is pretty wasteful.

The method is not a complete lie, it's just dangerous and expensive, like blowfish sushi or cave diving.

[Advertisement] Otter - Provision your servers automatically without ever needing to log-in to a command prompt. Get started today!