Daily Archives: February 26, 2006

You are browsing the site archives by date.

SynchronizationAttribute snake-oil

I have now been asked twice why you wouldn’t just use the [Synchronization] attribute when you want to make a class thread-safe. I figured I’d share what little information I have on this topic to you since I happen to have an answer. (as opposed to “the” answer which I never have).


As the saying goes, “There is no such thing as a free lunch.” I never really understood this saying as a kid. I grew up in a poor neighborhood and got “free lunches” all the time. Being financially challenged, I used to laugh and say “well, this is free”. As I’ve gotten older, and much more financially stable, I realize that my tax dollars are paying for the lunches for other kids in other neighborhood — apparently they are eating very well.


The same holds true for this special attribute. If you may think you are getting thread-safty for free, I have some snake-oil to sell you. There are several problems with the SynchronizationAttribute. Take a look at the required signature for use of the synchronization attribute:


[Synchronization]
public class SuperSensativeClass :
System.Runtime.Remoting.Contexts.ContextBoundObject
{
// Implementation here
}


First, the attribute requires that you inherit from ContextBoundObject — wasting your single-inheritance chain. If you wanted it in your object your most primitive object in the inheritance chain would have to derive from this class, and then what if you didn’t want some other child classes to be context bound?


Second, suppose your class has several methods which don’t have thread-sensative data. What happens there? The CLR will still lock those methods since it has no idea what is thread-volatile and what isn’t. This can severely hamper performance.


If you are looking at thread-safety, you are likely trying to optimize the perceived performance of your application using multi-threading. The SynchronizationAttribute can actually have the exact opposite effect. Long story short, learn to use the synchronization primatives of System.Threading.

Lessons in backups

OK, so I’ve re-learned the importance of not only backing up, but verifying backups. The other day, I decided to upgrade my blog software to CommunityServer 2.0. I apparently messed something up and before you know it, I had deleted my CS 2.0 database. While I had backups from a week ago (sufficient to restore back to my last blog post for sure), I had never verified the backups. Let’s just say the time to test your backups is not after a failure in which you need to use them.


Luckily, I had a backup device that most people don’t think of when they consider their options — Google Cache. I was able to easily use google cache to reassemble my blog posts as well as the comments. If some of you have noticed that my blog has been posting a large amount of posts in the past few days, its because I had to repost everything on my blog. Its a big pain in the butt and now I have to do some work to update google with my new articles, but at least I could recover it. Now before any Microsofties yell at me, I did try to use MSN Search cache first, but wasn’t able to find everything — one again proving that Google still has the edge in this market.


Suffice to say that this should be a lesson to all of you, and should serve as a refresher course for myself, that simply doing a backup isn’t sufficient — verification of that backup is essential to a good disaster recovery plan.