Threading deep dive – Day 5

When to go for multiple threads?

The general guidelines as to when to go for Multi threading depends on two important things. The number of processor cores present in the system and the type of work the application is intending to do. The general rule of thumb is we should have at least one thread for each virtual processor. Here the virtual processor represents a real processor or a core from the processor. The decision to go for more than one thread per processor depends on the type of the work the application is want to do. If the method that the thread needs to execute is highly processor intensive , then having more than one thread to execute on the same virtual processor will decrease the performance rather than increasing it. You know the reason now. What else can it be? Context switch. However if the method to be executed has lot of IO and file operation , using multiple threads will result in better performance with increased parallel processing and better user experience.

   

How many threads I can create in my program?

You can create as many as you want as long as you have sufficient resources. But would you really want to create so many threads. During application design you should not consider, how many threads that you can create and so on. If it is one of the design criteria , then think of redesigning the application. Jeffrey Richter writes wonderful column on MSDNMAG called "Concurrent affairs". It is a must to read for all parallelism and threading lovers. I remember seeing this kind of question regarding application design and Jeff's answer in that column.

   

What are Thread stack and its size?

But when you create threads you should consider the default size of the thread stack. When you create threads using CreateThread API, you can specify the maximum stack size for the thread. MSDN says "By default, the maximum stack size of a thread that is created by a native Microsoft Internet Information Services (IIS) process is 256 KB. For example, Inetinfo.exe, DLLHost.exe, or W3wp.exe creates a thread, the maximum stack size of the thread is 256 KB by default. You can also explicitly call the CreateThread function to specify the stack size of the thread. In Microsoft Windows 2000, if the Microsoft ASP.NET Worker Process (ASPNet_wp.exe) creates a thread, the maximum stack size of the thread is 1 MB." From these excerpts it is clear that max size of managed thread is 1MB. If we create 1000 threads then we are eating up 1GB for just creating threads.

   

The problem with global variables on Threads

Remember that we were discussing on why processor brings variable values from RAM to processor registers. It is done for faster access. So the processor can process the values faster than accessing from RAM. But this performing action comes with another issue. When the processor takes the copy of the data into its registers, the original copy remains in the ram and this causes the problem. Assume that your program has two threads. One thread took the shared data value from the ram and keeping it in the processor register and start processing it by changing the value. Meantime, the quantum of the thread elapsed. So the outgoing thread wraps-up and the new incoming thread executes some other code that modifies the same data value from the ram. Here the Thread2 [T2] also bring the data from RAM and modifies that. Finally whoever writes last into the RAM wins the write and other thread loses its changes to the value. This is where thread synchronization comes into picture. As you may know synchronization is not required for method local variables. It is required only for global variables which are share by methods. Why? Because all method level variables exist within method level scope and are stored in thread stack.