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!

Remy Porter

Source link

You May Also Like

A Simple List Copy

Mike‘s team had a new hire. They did great on the interview,…

Crime Is No Laughing Matter, Officer Incompetent

Crime is about as amusing as trying to fit a cow into…

There’s Photoshop, Then There’s This

“Our Christmas card in 2007. Our daughter was photoshopped in, as she…

Russian President Putin Threatens To Totally Destroy Paris, France With His New, Powerful Y-7 Super Missile

Thursday, 15 December 2022 MOSCOW, Russia – (Satire News) – The Kremlin…