Be Lazy, but be ware of initialization exception

.Net 4 intro­duced the Lazy<T> type which allows you to cre­ate an object that can be lazi­ly ini­tial­ized so that you can delay the cre­ation of large objects, for instance. How­ev­er, if your ini­tial­iza­tion log­ic has the poten­tial to except at run­time (e.g. time out excep­tions read­ing from some exter­nal data source) then you should …

Be Lazy, but be ware of ini­tial­iza­tion excep­tionRead More »

Takeaways from Gael Fraiteur’s multithreading talk

After watch­ing Gael’s recent Skills­Mat­ter talk on mul­ti­thread­ing I’ve put togeth­er some notes from a very edu­ca­tion­al talk:   Hard­ware Cache Hier­ar­chy Four lev­els of cache L1 (per core) – typ­i­cal­ly used for instruc­tions L2 (per core) L3 (per die) DRAM (all proces­sors) Data can be cached in mul­ti­ple caches, and syn­chro­niza­tion hap­pens through an …

Take­aways from Gael Fraiteur’s mul­ti­thread­ing talkRead More »

SmartThreadPool – What happens when you take on more than you can chew

I’ve cov­ered the top­ic of using Smart­Thread­Pool and the frame­work thread pool in more details here and here, this post will instead focus on a more spe­cif­ic sce­nario where the rate of new work items being queued out­strips the pool’s abil­i­ty to process those items and what hap­pens then. First, let’s try to quan­ti­fy the …

Smart­Thread­Pool – What hap­pens when you take on more than you can chewRead More »

ThreadStatic vs ThreadLocal<T>

Occa­sion­al­ly you might want to make the val­ue of a sta­t­ic or instance field local to a thread (i.e. each thread holds an inde­pen­dent copy of the field), what you need in this case, is a thread-local stor­age. In C#, there are main­ly two ways to do this. Thread­Sta­t­ic You can mark a field with …

Thread­Sta­t­ic vs ThreadLocal<T>Read More »

.Net Threading — BeginInvoke uses the thread pool

For a while now I’ve been curi­ous as to whether the CLR uses the Thread­Pool to exe­cute a del­e­gate when Begin­In­voke is called: Whilst com­mon sense dic­tates that this must sure­ly be true, I couldn’t be cer­tain since I haven’t man­aged to find any con­fir­ma­tion in the doc­u­men­ta­tions. Thanks to Jon Skeet and Jeff Ster­nal …

.Net Thread­ing — Begin­In­voke uses the thread poolRead More »

Threading — introducing SmartThreadPool

As I’ve men­tioned in my pre­vi­ous post, the biggest prob­lem with using the .Net Thread­Pool is that there’s a lim­it­ed num­ber of threads in the pool and you’ll be shar­ing with oth­er .Net frame­work class­es so you need to be on your best behav­iour and not hog the thread pool with long run­ning or block­ing …

Thread­ing — intro­duc­ing Smart­Thread­PoolRead More »

Threading — using the ThreadPool vs. creating your own threads

There are a lot of dis­cus­sions on the pros and cons of using the Thread­Pool and cre­at­ing your own threads. Hav­ing spent a bit of time read­ing what oth­ers have to say, here’s a sum­ma­ry of the things I’ve picked up on. The prob­lem with cre­at­ing your own threads Cre­at­ing and destroy­ing threads has a …

Thread­ing — using the Thread­Pool vs. cre­at­ing your own threadsRead More »

Threading — Producer-Consumer Pattern

Hav­ing run into a bit of dead­lock­ing issue while work­ing on Bingo.Net I spent a bit of time read­ing into the Pro­­duc­er-Con­­sumer pat­tern and here’s what I learnt which I hope you’ll find use­ful too. AutoRe­setEvent and Man­u­al­Re­setEvent To start off, MSDN has an intro­duc­to­ry arti­cle on how to syn­chro­nize a pro­duc­er and a con­sumer …

Thread­ing — Pro­duc­er-Con­sumer Pat­ternRead More »

Threading — when to use Monitor.Pulse and Monitor.PulseAll

Ever won­dered when you should use Monitor.Pulse and when you should use Monitor.PulseAll giv­en that only one thread will be able to acquire the lock even if you wake up mul­ti­ple threads? I did, and that’s when I stum­bled across a sim­i­lar ques­tion on Stack­Over­flow and as ever, Jon Skeet came up with a real­ly …

Thread­ing — when to use Monitor.Pulse and Monitor.PulseAllRead More »

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.