How is C# threading like peeling potatoes?

I’ve been working on the Threading lessons for the C#, ASP.NET and Visual Studio Expert Skills book, and an interesting analogy struck me.

I have a sack of potatoes that need to be peeled as quickly as possible. I also have two chefs, Jo and Bill, who are in kitchens on opposite sides of a building.

In the first scenario, I deliver the potatoes to Jo and Bill one at a time, running backwards and forwards from the sack. It takes so long for me to carry each potato to the chefs that it would actually be faster to peel them myself.

This is the same when working with C# threads. Using two threads to accomplish a lot of small tasks is actually slower than doing all of the tasks using a single thread because of the overhead that’s added by managing the threads.

This code uses the “single potato” approach to check prime numbers up to MaximumValue:

while (CurrentValue < MaximumValue)
{
    if (!FirstThread.IsAlive)
    {
        FirstThread = CreateThread();
        FirstThread.Start(CurrentValue);
        CurrentValue++;
    }
    if (!SecondThread.IsAlive)
    {
        SecondThread = CreateThread();
        SecondThread.Start(CurrentValue);
        CurrentValue++;
    }
    Thread.Sleep(1000);
}

This code gives each prime number to the two threads individually. The prime numbers are the ‘potatoes’ in this instance. Because there’s so much overhead starting and stopping the threads, this code is actually much slower than it would be if it didn’t use any threading code at all!

So what’s the solution to the potato problem?  Well of course, it doesn’t make any sense to carry the potatoes one at a time. It would be much more logical to give half of the potatoes to Bill and the other half to Jo. That way they can do their jobs and there’s no time wasted running backwards and forward from the potato sack.

In C# terms, that means our solution is to divide up the prime numbers between our two threads and have each thread process them all in one go instead of starting a new thread for each number.

Here’s how the code looks with the prime numbers divided up between the two threads.

Thread Thread1 =
new Thread(new ParameterizedThreadStart(CalculatePrimes));
Thread1.Start(new int[]{2,StartingPointForThread2-1});
Thread Thread2 =
new Thread(new ParameterizedThreadStart(CalculatePrimes));
Thread2.Start(new int[] { StartingPointForThread2, MaximumValue });

while (Thread1.IsAlive || Thread2.IsAlive)
{
    Thread.Sleep(1000);
}

In this code, the two threads only start once and don’t need to restart for each prime number. This implementation is much faster than working with a single thread (or peeling all of the potatoes yourself!).

There’s a lot more about threading in the Expert Skills book, including using the ThreadPool class to manage threads more easily. ‘Learn ASP.NET 4.0, C# and Visual Studio Expert Skills with The Smart Method’ will be available for purchase around June 2012.

Expert Skills Session 1 Finished

Continuing on with Expert Skills, I’ve now finished Session 1 and sent out a proof to my proofreaders. If you’re on of them, you should have it by now!

Session 1′s taken longer than I expected, but that’s mostly down to the amount of time I’ve spent working on the sample files for the new course. Being Expert Skills, the sample files need to make use of far more complex features than those used in Essential Skills.

So far I think the Expert Skills course does a great job of applying to real-world scenarios. Once it’s finished I’m confident that the two books will be the absolute best way to learn ASP.NET, C# and Visual Studio.

Getting started with Expert Skills

Now that the Excel 2010 Expert Skills video course is complete, I have begun work on the ASP.NET 4.0, C# and Visual Studio 2010 Expert Skills book.

If you’re a proofreader, you can expect to start receiving PDF versions of the first drafts in the coming weeks.

I have also revised the course outline, adding an entirely new session on compiler directives and advanced debugging techniques. The outline is likely to continue to change as I receive more suggestions from our readers.

If you have a suggestion for something you’d like to see in the Expert Skills course, get in touch and let me know!

Excel 2010 Expert Skills video course now available

Thanks to a lot of hard work, The Smart Method’s new Excel 2010 Expert Skills video course is now available for download.

We’ve also added some free video lessons from the Expert course to the Excel 2010 website and our YouTube playlist.

Expert Skills course outline now available

You can now read the outline for the Expert Skills course by clicking here.

This outline isn’t absolutely final yet, so if you have any comments or suggestions for topics to be included, let me know.

YouTube Playlists Now Available

Although it’s still better to view the free videos on the official website, you can now also view each of the complete courses as YouTube playlists. YouTube playlists group all of the video lessons together, making them much easier to find on YouTube.

Free ASP.NET, C# and Visual Studio Video Course:
Official Site  YouTube Playlist

Free Excel 2010 Video Course: Official Site YouTube Playlist

Free Excel 2007 Video Course: Official Site YouTube Playlist

Free Excel 2010 Course Now Available

We’ve been working hard over the past few weeks and The Smart Method’s new Excel 2010 free video course is now available here.

Over the coming weeks we’ll be adding more Excel 2010 videos, and we hope to have the complete Essential Skills course available by the end of the month.

Cellular Automata Program: Life

A program I wrote called “Life” is now available from the downloads page. You can also read a little more about it on the Cellular Automata page.

Life was written in C# using Visual Studio. The full source code for Life can also be downloaded from the downloads page.

Additional videos now available

As well as the 41 lessons in the free starter course, you can now view a selection of lessons from the later sessions.

You can find them on the free tutorials page.

Complete video course now available

The complete video course is now available for instant download.

You can download a copy for your country by clicking here.