Spider's Web: Spawning threads in .NET (Part 1 of 5), The Basics

Now with our multi core CPUs and high expectations of application responsiveness, executing the application in multiple paths have become a requirement rather than an option. Also running the code in application servers requires that we know a thing or two about threads. So I have decided to write a few posts about threads and their use in .NET environment. This is the first part of a five part series post.

History

A process in traditional programming languages pursued one execution path. Unix platform provided the fork command which would enable one to split the execution into different paths. A spawned process could have independent execution path but that required quite an amount of resources. A thread is a simpler and cheaper way of having different execution path at real time with the benefit shared memory space. Each process has a minimum of one thread. With the invention of threads, the code blocks of a same process could run virtually simultaneously. With today's multi core CPUs the 'virtually' part of running simultaneously can be taken out and a thread can in physical world run simultaneously. The implementations of threads are different on different operating systems. For more historical information on threads please refer to the Wikipedia Article.

The Basics

System.Threading namespace contains the classes required for threading. This is the basic syntax of spawning a  thread.

Thread thread1 = new Thread(new ThreadStart(function));

A thread takes a ThreadStart delegate as the constructor parameter. A Delegate is a safe function pointer. (For more information on delegates please visit these next links MSDN delegate reference and MSDN Article: An Introduction to Delegates from Jeffrey Richter.) 

After creating the thread instance, call the start method to start the thread.

thread1.Start();

The code sample below creates a sample thread and runs it.

class Program
{
    static void Main(string[] args)
    {
        WorkerObject wo = new WorkerObject();
        Thread thread1 = new Thread(new         thread1.Start(); ThreadStart(wo.Count));       
        for (int i = 0; i < 100; i++)
        {   
           Console.WriteLine("Main says, I am counting ...{0}", i);
        }
    }      
}

class WorkerObject
{
    public void Count()
    {
        for (int i = 0; i < 50; i++)
        {
            Console.WriteLine("Worker object says, I am counting ...{0}",i);                        }     }
}

This is a very simple example of threading where the main application thread prints out a number to console every second and the another thread is printing out a number to console 1/10 of each second.

The application output looks like the this

Main says, I am counting ...0
Main says, I am counting ...1
Worker object says, I am counting ...0
Worker object says, I am counting ...1
Worker object says, I am counting ...2
Worker object says, I am counting ...3
Main says, I am counting ...2
Main says, I am counting ...3
Main says, I am counting ...4
Worker object says, I am counting ...4
Worker object says, I am counting ...5
Worker object says, I am counting ...6
Worker object says, I am counting ...7
Main says, I am counting ...5

If we observe the output then we can see that both execution path are running at the same time. Several points to note here.

  • There is no time deterministic way of scheduling code executing in a thread. When there are multiple threads the OS will decide which time slice to give which thread and when. Also the time slices are not equal.
  • After calling a thread start from another thread the call will return control immediately that other thread will start at its own time which can be immediate or later

Waiting using Thread.Sleep()

Thread.Sleep() is a static function which causes the executing thread to pause for specified number of milliseconds. It it quite useful when you need to pause processing for a while. It must be noted that Thread.Sleep() will only pause the executing thread which called the Sleep function.

Wait for a Thread to finish by using thread.Join()

Join() is an instance function available to a thread. This blocks the calling thread until the target thread has finished.

 class Program
{      static void Main(string[] args)
     {
         WorkerObject wo = new WorkerObject();
         Thread thread1 = new Thread(new ThreadStart(wo.CountSlowly));
         Console.WriteLine("Before starting the thread.");
         thread1.Start();
         thread1.Join()
         Console.WriteLine("After starting the thread.");
     }
}


class WorkerObject
{
     public void CountSlowly()
     {
         for (int i = 0; i < 5; i++)
         {
             Console.WriteLine("Worker object counts...{0}",i);              Thread.Sleep(1000);          }      } }

This code sample demonstrates the use of static function Thread.Sleep() and instance function Join(). The application started a thread waits for thread to finish by calling thread1.Join(). The function CountSlowly waits 1000 ms before counting each number by putting the thread1 to sleep. The application output looks like this ...

Before starting the thread.
Worker object says, I am counting ...0
Worker object says, I am counting ...1
Worker object says, I am counting ...2
Worker object says, I am counting ...3
Worker object says, I am counting ...4
After finishing the thread.

Passing parameters to the thread function

Before .NET 2.0 the function passed to the thread had to be a void function without any parameter. The basic idea was to pass the parameters to object via private variable. However that creates a problem for static functions or instances of objects stored in static variables, which I will discuss in a later part. So we can modify our WorkerObject class to store a parameter. The code should look like this

class WorkerObject
{
    string _name = string.Empty;
    public WorkerObject(string name)
    {
        _name = name;
    }
    public void CountSlowly()
    {
        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine("{0} says, I am counting ...{1}", i, _name);
            Thread.Sleep(1000);
        }
    }
}

Now we can use write code that creates the Worker class with the constuction parameter which may look like this

  WorkerObject wo = new WorkerObject("Bumble Bee");
  Thread thread1 = new Thread(new ThreadStart(wo.CountSlowly));
  thread1.Start();

This way we are keeping the function signature same while passing the parameter to the object. This is a dangerous practice if the object instance is being shared among different threads at the same time, otherwise it is harmless. From .NET 2.0 we have a new feature of passing parameters. In order to pass parameters we need to use ParameterizedThreadStart() delegate to pass parameters to a thread function. The signature of the function should look like this

  void function (object)

Now only one parameter can be passed to the function. So if we need to pass multiple parameters then we will need to create a custom object that holds the parameters or we can use a dictionary. Also inside the function we need unwrap the object to a strong type and work with it. In the next example we are going to pass two parameters to the function.

// Custom parameter type 
class
CountParams
{
    string _name = string.Empty; int _max = 0;
    public CountParams(string name, int max)
    {
        _name = name;
        _max = max;
    }
    public string Name { get { return _name; } }
    public int Max { get { return _max; } }
}

class WorkerObject
{
    public void CountSlowly(object data)
    {
        // Unwrap the parameters         CountParams parms = (CountParams)data;         for (int i = 0; i < parms.Max; i++)
        {
            Console.WriteLine("{0} says, I am counting ...{1}", i,
               parms.Name);
            Thread.Sleep(1000);
        }
    }
}

As we can see that we need pass two parameters to the function name and how many times will we count, so we have created a class to hold multiple parameters called CountParams. Then inside the function we unwrap the object to our parameter class and use it. Please note the if we had only one parameter to pass to the function like and integer then we would not need to create this parameter class and would be able to directly cast the object to int. Lets observe how we are calling the function ...

WorkerObject wo = new WorkerObject();
Thread thread1 = new Thread(new ParameterizedThreadStart(wo.CountSlowly));
thread1.Start(new CountParams("Bumble Bee", 10));

Now the above example shows us that we construct the thread with a ParameterizedThreadStart delegate and then in thread.Start() function we are passing our custom parameter object.

There is something called Thread Local Storage (TLS) which I will cover in a future part in the thread post series.

Multicasting

One more thing to remember that the delegate for ThreadStart is a like normal multicast delegate. So we can assign more than one function to the delegate. But that will not call each of the functions simultaneously rather call them on the same thread one by one. The example below shows sample multicasting  ...

WorkerObject wo = new WorkerObject();
WorkerObject wo2 = new WorkerObject();
ThreadStart ts = new ThreadStart(wo.CountSlowly);
ts += new ThreadStart(wo2.CountSlowly);
Thread thread1 = new Thread(ts);
thread1.Start();

In the sample above the thread will call wo.CountSlowly and when the function call finishes then will call wo2.CountSlowly. This is how we can use multicasting while threading.

With this I will close this post. The next post on threading will contain information on DataSlots, locking and some synchronization.

kick it on DotNetKicks.com


Profile your performance worries with DotTrace 3.0 profiler

We have a large database for the website back at work and also we have a high amount of disk IO and we were using the ASP.NET membership provider that comes built-in. We replaced the ASP.NET provider as it is not suited to a large scale application and has quite a lot of performance issues. For example it creates an anonymous user if you just hit the site ... which is true for even bots. Think of the amount of garbage user you can get with ASP.NET membership.

After deploying the performance improvement to the site we saw that the CPU usage to the site became 3 times higher than usual ... not much of an improvement ..eh! However from the changes made to the code it was obvious that there should not be higher CPU usage rather it should be low.

I downloaded a version of the DotTrace profile from Jetbrains. Its an amazing profiling tool. We have several web servers, I took one of the servers out of the load balancer. When you profile an ASP.NET app, the profile restarts the IIS. During that time we do not want the users to experience error or no page so its best to take the web server out of the load balancer. Turned on the profiler and then put the server back into the load balancer so that it can serve pages and encounters real life load. After a few minutes took a snapshot of the server. All this time CPU was high.

DotTrace is an amazing tool and it shows amount of time and the amount of calls and % for all methods and all methods called by your application.

Note: Some of the method names in the images are renamed or hidden for security and copyright reasons.

Img_1

As one can see that the process request is taking 8.8% time which is normal but Application_AuthenticateRequest is taking 3.3% which is very strange.

Investigation showed that a method used to identify cookies from domain is taking huge time (see below). The code uses regular expressions.

Img_2

Since our website calls itself internally from the server side from various requests generating a cookie for server side call should be wrong and this method validates if this is not a localhost call.

 

Then I investigated the code and found the following

private static bool IsValidDomainName(string inputString)
{
    System.Text.RegularExpressions.Regex regex =
    new System.Text.RegularExpressions.Regex(@"^[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)$",
    System.Text.RegularExpressions.RegexOptions.IgnoreCase |
    System.Text.RegularExpressions.RegexOptions.Compiled);

    return regex.IsMatch(inputString);
}


There are 2 things with this code. First of all this is a static function with a static expression validate with so the regex object can be static.

Second, the regex.IsMatch is a expensive function when it is in a area with high code coverage.

We just replaced this code with simple string matching and we our CPU just went down, see the web server CPU usage.

Bad_good_cpu

Click on the image to the high CPU usage vs after the applying the patch.

Dotrace can both CPU profile or memory profile an application and also have some cool views that you find bug fast.

It is evident that entry point of the request must me properly optimized as it called so many times. There is no chance for mistake for a code with huge coverage like that.

kick it on DotNetKicks.com

Interesting attributes in CompilerServices namespace

The namespace System.CompilerServices has a few useful classes that can be used by a developer even though he is not working on compilers.

SuppressIldasm Attribute

One interesting element could be to stop disassembling your code by using this attribute. This does not obfuscate your code code rather tells the ILDASM to not show your code when IL is being decompiled. This can be applied on an Assembly.

Example

Just add this line to your code.

[assembly: System.Runtime.CompilerServices.SuppressIldasm()]

StringFreezing Attribute

If you apply this attribute to the assembly then all the strings used within the assembly is frozen when native generation is used. This is useful of a performance hungry application that uses a lot of strings. Warning, be sure to apply this to the assemblies that do not require unloading as the assembly with this attribute cannot be unloaded.

Example

Just add this line to your code.

[assembly: System.Runtime.CompilerServices.StringFreezing()]

Just add this line to your code.

 

Technorati Tags: ,

Independent Thought

The web development increases the door towards internet global world. This internet world enhances the field of MBA and affiliate marketing line for companies. Different companies enhance their promotion and marketing by website design holding their product promotion. Different famous companies enhance small firms by introducing affiliate program for them. These companies have started their separate computer department specially working for web hosting services. Different services are provided other than web hosting and this include internet marketing solutions for the firms. Due to all these services through internet the field of web design development becomes very famous.


This week's musings: Time saver freeware

 

image Drop your SnagIt or any other screen Capture software and start using the software Jing Project, which by the way is a project of Techsmith the creators of SnagIt screen Capture. This is so far the best screen capture software on the Internet because its user friendly and has the bare minimum features that you need.

image Sumatra PDF is 750KB pdf reader that loads very fast and has a light footprint. I have just switched to it from the Adobe Viewer. If you want search feature then stick to Adove Acrobat Reader.

 

Skylight is a shortcut launcher program like Launchy but it is written in .NET 3.0. You can see an image below.

 

Skylight


Loading ISO files with Virtual Drive for XP from Microsoft

Microsoft now releases beta version of Visual Studio 2008 and other beta products as iso images. Unfortunately some of these iso files are in non standard format and cannot be loaded by some iso reader software. However Microsoft has an unsupported virtual CD loader. It loads iso files as a virtual CD Rom drive. This software can be found here

http://download.microsoft.com/download/7/b/6/7b6abd84-7841-4978-96f5-bd58df02efa2/winxpvirtualcdcontrolpanel_21.exe


New Opera 9.5 beta release on 25th

Opera_logo3 Opera 9.5 Alpha has been released and it had quite a lot of issues but it has faster JavaScript engine and a whole lot better history search. I have been using opera since version 3.00 and absolutely love this product. I still consider it to be the best browser in the world. Well ... I am a Opera Maniac.

Opera 9.5, codename Krestel will go beta on 25th October, with a large boom in San Francisco. More details can be found here. You can also follow the development progress in the Opera Desktop Blog.

One of the cool things that Opera supports now, is a tag called Canvas which is a part of HTML5 specification. Unfortunately all the idiots who still use Internet Explorer won't have this. Canvas is basically like a Java canvas element where one can draw shapes using JavaScript. How you can use Canvas in Opera can be found at this article in the Opera developer site. All the Major browers (Firefox, Safari, Opera) except Internet Explorer supports canvas. A tutorial for <canvas> tag usage can be found at Mozilla tutorial. I still can't believe Microsoft is doing such a sloppy work with the IE browser, its a bad strategy. FF has kicked IE's butt big time, still the giant is sleeping.

One cool implementation with Canvas and Javascript can be found at this site but it requires the use of Opera 9.5 beta or Firefox or Flock since Opera 9.xx version before 9.5 cannot render it properly. But its nice to see a painting application using pure html.


Working women of Bangladesh write blogs

The organization called Narijibon has put up a blog at which is featuring the working women from Bangladesh and the  story of their life. Most of the entries involve life story of the women who have had aid from the organization and how they have grown up, life changing struggles, life, marriage and death etc. Some entries are quite touching. Unfortunately there is no English translation available yet. If you can read Bangla please visit the blog, it may be an eye opening experience for the good life we live in and have taken for granted.


10 Software that I can't live without

1. Total Commander (Best file manager ever): I cant believe anyone can live without Total Commander ( previously known as Windows Commander) which I have been using for over 10 years. I have forgotten how to use windows explorer which is 10 times slower in any kind of operation. This one file manager software lets me do everything. And by everything I mean "Everything". For example it lets me do the following

    1. File Management Tasks: File and Folder create copy, delete and all other regular stuff
    2. Compress to any form of archive, be it zip, rar, ace, 7z ... name anything.
    3. Compare files and folders and edit them
    4. Burn my CDs
    5. Install uninstall any softares
    6. ftp client ( has email, rss, nntp clients but I dont use them)
    7. Start, stop, install uninstall windows services
    8. Encrypt, split files
    9. Manage processes, Edit registry
    .... there are more but I don't have the time to write it down.   

2. Opera (Best web browser): Using opera from version 3 and still hooked. Nothing comes close to it. I have recently started to use Flock instead of Firefox for the sites that run better in FF.

3. Thunderbird (Mail): Used to be a total fan of Opera mailing client M2, but recently moved to thunderbird for all the plugins. Outlook sucks big time!

4. Skype (IM): Most of  my official contacts are in skype

5. Facebook (www.facebook.com): Found all my friends from grad school in Facebook. Visit daily. I am in love with this site

6. STablauncher : A windows based tabbed launcher, most of the programs that I use daily are located in this software

7. Google : No need to say why.

8. .NET Reflector: .NET Code reflection and decompilation tool.

9. Code Project : Its the best programming site.

10. LifeHacker.com: Started to hack my life.


Trash your Tv

Recently one of my favorite tv shows, Smallville has kicked off to a whooping season 7 in the US. But fortunately to the wonders of the internet I can watch them a few hours just after the show has aired in the US. One of the issues with Television is that it airs a certain program at a certain specific time. And nowdays its quite impossible to watch them at that time because I am busy. Also its kinda hard to wait for the whole week to see an episode. Stargate SG-1 had just ended after its 10th seasons running and Stargate Atlantis has started season 4.

Now you everyday TV does not serve these programs at your region at your chosen time. Trash your tv, its worthless. The only thing it will do is to eat up your time when you dont want it to. If you want to watch something watch it over internet, so that you are not slave to the timing. You can watch it over the weekend when you are free.

Talking about this reminds me to write about Joost, a peer to peer based high definition internet TV program. It has over 100 channels with various interests and programs. Try it, you'd really want to throw away the time limiting Television in your living room,


Amazon S3 Webservice: The best storage deal on the planet

AmazonS3 Since I have several widgets from different sites which have images served from different websites I was worried about the download time and decided to look for CDN (Content Delivery Network) service. The CDNs have distributed servers all over the world.

Amazon has this web service called S3 (Amazon Simple Storage Service) which is basically a paid CDN. But Its unbelievably cheap. The service supports REST and SOAP web services. Unlike other services there is no bulk purchase, you pay for what you use. If I wanted to host some files this service and had a big bandwidth connected to my home server, the electricity cost alone would be higher than this web service. If you store 20 Gigabytes of data and have a monthly traffic of 2GB then it will cost you 3.40U$. I have a few hundred megabytes of file to store and my traffic probably will be less than 10 cents a month. Pricing from Amazon is like this:

  • $0.15 per GB-Month of storage used
  • $0.18 per GB - first 10 TB / month data transfer out
  • $0.01 per 1,000 PUT or LIST requests
  • $0.01 per 10,000 GET and all other requests*

And the best thing is that, this is a fault tolerant, scaled, reliable, fast service at such a cheap price. I can host all my personal files here and still it would be better than hosting somewhere else. Amazon provides API with code samples for all major programming languages (C#, Java etc). Since these files are added, modified and fetched via API then how do I host my images, movies etc there without writing code? Well, the answer to that is pretty simple, create a bucket (like a folder) and put your files in there and make them public and then you can access them with http gets (read on for how to do that). The Amazon web services logo image used at this blog post is served from the Amazon S3 service.

Nice, but what else can I do with it?
You can even use the S3 service to as a web storage with JungleDisk (see the Amazon traffic rate at their site) and use it as a web hard drive, but the software costs 20 U$, so it is several times more expensive than my 1 years of traffic at Amazon S3. I am not going use it. Instead ...

I stumbled onto this Firefox extension which is like a file transfer utility to S3. You can find the extension here and also read this article at coding horror.

What do I plan to do with this service
I am planning to write a free program for Amazon S3 service which probably will kick JungleDisk out of business and store my files there. Since I have started regular office hours in office recently and stopped bringing work home, I think can manage to write this at home after work. But as I have so many unfinished pet projects to finish ... lets see when I get time for this.