What Every Dev Must Know About Multithreaded Apps

Pay attention to this few lines of code:

class Calculator {
    private static int m_TotalCount;


    public void DailyCut(int
limit)
    {
        m_TotalCount = 0;
        while
(m_TotalCount != 0 && m_TotalCount < limit)
        {
            // some process
            m_TotalCount++;
        }
    }

    // other methods
}

Let's focus in just one of the several silly things of the sample: after setting m_TotalCount to 0, we immediately check whether m_TotalCount is different from zero, which will never be true, so the process inside the loop will never be executed. Obvious, right?

Now, what would you do if you find out that the process, however infrequently, does get executed? A bug in .NET? Bill Gates? Well, far more plausible is that we are facing a concurrency problem: if several execution threads run through the Calculator class, then may be some method inside thread Y changes the value of m_TotalCount, right when thread X is between the assignment and the while, improbable but possible.

This is an example of the bad things that can happen in multi-threaded environments, that's why it is recommended that we rookies better stay away from these waters, but at the same time multi-threaded programming offers several tempting advantages and, besides, with the generalization of multi-core and multi-process machines, it's going to be an unavoidable fact of life. That's why we better learn the ways and means of multi-threading and a good starting point is this nice article by Van Morrison that won an Excellence Award from the Society for Technical Communication. A very short and recommended reading.

Published Tuesday, June 13, 2006 1:07 AM by Edgar Sánchez

Comments

# re: What Every Dev Must Know About Multithreaded Apps

Tuesday, June 13, 2006 2:46 PM by fx
One little trick which not too many devs no about is thread locals, which would solve the problem here without needing to write synchronization code:

[ThreadStatic]
private static int m_TotalCount;

The only time you would need to be careful about using threadstatic is withing thread-agile runtimes, ie. runtimes that switch execution of a request to different threads (ASP.Net, so the rumour has it, is thread-agile)

# re: What Every Dev Must Know About Multithreaded Apps

Friday, March 23, 2012 6:05 PM by URMEHEHEdl

kUMDbL I really like and appreciate your article.Really looking forward to read more. Really Cool.

# re: What Every Dev Must Know About Multithreaded Apps

Tuesday, April 03, 2012 7:36 AM by ciuLoXqKwmXmWcYkVfL

I really like and appreciate your post.Much thanks again. Awesome.

# re: What Every Dev Must Know About Multithreaded Apps

Tuesday, April 03, 2012 8:08 AM by TGQBTSnonav

A round of applause for your article post.Really looking forward to read more. Really Cool.

# re: What Every Dev Must Know About Multithreaded Apps

Tuesday, April 03, 2012 8:54 AM by sFQycJKXHKTwXz

Major thankies for the blog post.Thanks Again. Cool.

# re: What Every Dev Must Know About Multithreaded Apps

Tuesday, April 03, 2012 9:38 AM by iePUuTFOtmde

Awesome blog post.Really looking forward to read more.

# re: What Every Dev Must Know About Multithreaded Apps

Tuesday, April 03, 2012 12:11 PM by IOZDTrqpiQlkPDo

Hey, thanks for the post. Really Cool.

# re: What Every Dev Must Know About Multithreaded Apps

Thursday, April 05, 2012 6:35 AM by wcTBLcVmoRXHDbqEp

Thanks for sharing, this is a fantastic blog.Really looking forward to read more. Keep writing.

# re: What Every Dev Must Know About Multithreaded Apps

Friday, April 06, 2012 5:09 AM by KBRTEGdQpRz

Thanks for sharing, this is a fantastic blog.Really looking forward to read more. Really Great.

# re: What Every Dev Must Know About Multithreaded Apps

Friday, April 06, 2012 6:47 AM by CrpbrXWHWyLIVZXrY

Really appreciate you sharing this blog article. Great.

# re: What Every Dev Must Know About Multithreaded Apps

Monday, April 09, 2012 10:21 AM by gqiChDwBqxDFVr

Im grateful for the article post.Much thanks again. Fantastic.

# re: What Every Dev Must Know About Multithreaded Apps

Monday, April 09, 2012 10:51 AM by CdQQzmMpYTnTeb

I truly appreciate this article post.Much thanks again. Much obliged.

# re: What Every Dev Must Know About Multithreaded Apps

Tuesday, June 26, 2012 12:37 PM by computer repair software

I am typically to blogging and i really appreciate your content. The article has really peaks my interest. I'm going to bookmark your site and keep checking for new information.

# re: What Every Dev Must Know About Multithreaded Apps

Monday, July 09, 2012 8:34 PM by qtIUWCPURzpgsZ

PiIebW Enjoyed every bit of your blog article.Thanks Again. Cool.

# re: What Every Dev Must Know About Multithreaded Apps

Monday, September 24, 2012 7:35 PM by Grierhoom

There may be a fresh creation that everybody who smokes should really know about. It's identified as the ecigarette, often called a smokeless cigarette or <a href=www.vaporultra.com/>gamucci electronic cigarettes </a> , and it is shifting the legal panorama for cigarette smokers throughout the entire world.

The patented Ecigarette delivers to proficiently simulate the expertise of smoking an real cigarette, devoid of any on the well being or authorized concerns encompassing conventional cigarettes.

When E-cigarettes glance, come to feel and style similar to traditional cigarettes, they perform quite in different ways. You see, e cigarettes don't really melt away any tobacco, but rather, whenever you inhale from an e-cigarette, you activate a "flow censor" which releases a drinking water vapor containing nicotine, propylene glycol, and also a scent that simulates the flavour of tobacco. All of which merely means that e cigarettes permit you to get your nicotine deal with although evading each of the cancer leading to brokers located in traditional cigarettes these kinds of as tar, glue, countless additives, and hydrocarbons.

On top of that to being more healthy than traditional cigarettes, and maybe most importantly of all, is the proven fact that electric cigarettes are fully authorized. Due to the fact E-cigarettes do not involve tobacco, you could legally smoke them any place that common cigarettes are prohibited like as bars, dining establishments, the perform place, even on airplanes. In addition, electric cigarettes help you smoke without fears of inflicting harm on other folks due to nasty second hand smoke.

The refillable cartridges can be found in a multitude of flavors as well as nicotine strengths. You'll be able to get common, menthol, even apple and strawberry flavored cartridges and nicotine strengths can be found in extensive, medium, gentle, and none. While e cigarettes are technically a "smoking alternative" alternatively than a smoking cigarettes cessation unit, the selection of nicotine strengths gives some evident prospective as an aid during the kinds makes an attempt to stop using tobacco and looks to be proving well-liked inside that industry.

The nice matter about e cigs as apposed to say, nicotine patches, is the fact e-cigarettes produce the same tactile sensation and oral fixation that smokers wish, although satisfying types tobacco cravings as well. Any time you get a drag from n ecigarette you truly come to feel the your lungs fill having a heat tobacco flavored smoke and when you exhale the smoke billows outside of your lungs just like frequent cigarette smoking, nonetheless, as talked about, that smoke is definitely a a great deal much healthier water vapor that speedily evaporates and so is not going to offend any person within the fast vicinity.

When e cigs have been all around for a while in different incarnations, it's been new advances in the technological know-how and at any time rising restrictions from using tobacco which have propelled the e-cigarette right into a new found popularity. If you are keen on a healthier different to smoking, or for those who simply desire to hold the flexibility to smoke where ever and every time you need, an electric cigarette is likely to be the solution you've been on the lookout for.

# re: What Every Dev Must Know About Multithreaded Apps

Monday, November 05, 2012 12:59 AM by efqCuHULEnysa

JpvNlv Great post. Fantastic.

# re: What Every Dev Must Know About Multithreaded Apps

Saturday, February 02, 2013 3:23 AM by jNFZMkumghy

MkrSps I am so grateful for your post.Much thanks again. Cool.

# re: What Every Dev Must Know About Multithreaded Apps

Friday, March 01, 2013 1:43 AM by hAqufESFIB

nCVYsD Thanks for the blog post.Really looking forward to read more. Will read on...

# re: What Every Dev Must Know About Multithreaded Apps

Wednesday, April 17, 2013 3:51 PM by Trent

I for all time emailed this website post page to all my associates, as if

like to read it then my links will too.

Leave a Comment

(required) 
(required) 
(optional)
(required)