Saturday, May 31, 2008

Now! That has to be one of my greatest findings! Ever!

The Indexer method (the one that allows for doing object[something]) of the Hashtable class in .NET has a Thread.Sleep(1) inside one of the loops!!!!

Yes I repeat Thread.Sleep(1)!

Why would anyone want to insert a Thread.Sleep() call in to a Hashtable, or any other BCL class for that matter, is beyond me.

Can you beat this gem?

Sunday, June 01, 2008 7:03:02 AM (Central European Standard Time, UTC+01:00)
It's called "playing nice with others." You deliberately yield CPU ownership with Thread.Sleep(1) to allow another thread a chance to execute.

In this case, the get_Item code is giving another thread with write access to the hashtable an chance to finish. This way, the get method can check its hashtable bucket again after the write's done in case it changed because of the write.

Without Thread.Sleep(1) out, get_Item would waste a lot of CPU cycles constantly checking a bucket that isn't being given a chance to update, because we're hogging the CPU busy-waiting for it to finish.
Joe Chung
Sunday, June 01, 2008 10:14:17 AM (Central European Standard Time, UTC+01:00)
Since when do a BCL class code implicitly "plays nice with others"?

I mean, I understand the concept, but! No way I could accept such an explainationos.

Is there any other class that impplicitly does this?
Dictionary<TKey, TValue> maybe? Nope. List<T>? Nope.

Reason is: if you want your code to behave nicely in multi-threaded environement - code it yourself.
Monday, June 02, 2008 7:57:39 AM (Central European Standard Time, UTC+01:00)
there is a nice post of Joe Duffy (http://www.bluebytesoftware.com/blog/PermaLink,guid,1c013d42-c983-4102-9233-ca54b8f3d1a1.aspx) regarding Thread.Sleep(1)
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview