where developers meet development
Saturday,September 11,2010
CollabNet Intends to Become No.1 in Agile ALM by Acquiring Danube   Enterprise consolidation is the order of the day and here is another piece...
Micro Focus Integrates Visual Studio 2010 in Cobol Tools   Micro Focus has announced four new Cobol products to allow developers to...
ODA Open Design Alliance Announces DWGdirect.NET Beta Release   The Open Design Alliance (ODA) announces the beta release of DWGdirect.NET,...
Hot and Safe: a Beginner's Guide to Multithreaded Libraries   Most of the discussion of multithreading that emerges from Cilk Arts is...
8 Simple Rules for Designing Threaded Applications   Multithreaded programming is still more art than science. This white paper...
 

EXCLUSIVES 

Writing High Performance .NET Code

Threading Tips

a) Distribute the work equally among threads – If the work is imbalanced, one thread will finish the work quickly, but must wait till other thread(s) finish their job, impacting performance.

b) Don’t use too much shared data among threads – If any data or data structure is shared among threads, then synchronization is required to update that data. This increases the amount of serial code/paths in your application hurting your scalability

c) Acquire Lock late and release it early. This is very important as you must take a lock just before you absolutely have to and release it first before doing anything once the atomic region is executed. Here is an example in .NET


void foo ()
{
    int a, b;
    …. //some code
    //Following code has to be atomically executed
    {

    }
    …. //Some other code
    //End of atomic region
}


//WRONG: Increased atomic region. Lock will be held longer thus hurting performance
void foo ()
{
    int a, b;
    Object obj ; //for synchronization
    Monitor.Enter(); or lock(obj) {
    …. //some code
    //Following code has to be atomically executed
    {
    }
    …. //Some other code
    Monitor.Exit(); or }
    //End of atomic region
}
//WRONG: Entire function is synchronized. Bad idea.
using System.Runtime.CompilerServices;
MethodImplAttribute(MethodImplOptions.Synchronized)]
void foo ()
{
    int a, b;
    …. //some code
    //Following code has to be atomically executed
    {
    }
    …. //Some other code
    //End of atomic region
}

//Correct: Synchronizing just that block which needs atomic execution
void foo ()
{
    int a, b;
    Object obj;
    …. //some code
    lock(obj) {
        //Following code has to be atomically executed
        {
        }
    }//end of lock
    //End of atomic region
    …. //Some other code
}


d) Use proper synchronization primitives: There are multiple synchronization primitives that are provided by .NET Framework. These vary from fewer features (very fast) to many features (very slow). It is important to use this correctly to get optimal performance. Synchoronization primitives can be defined as:

  d.a. Monitor or lock: Provides a mechanism that synchronizes access to objects
  d.b. Interlocked: Provides atomic access to variables that are shared by multiple threads. For example: for any atomic ++ or –- operations consider using Interlocked class
  d.c. Mutex: Synchronization primitives that can be used for inter process synchronization. They are considerably slower; use it when you absolutely need it.
  d.d. ReaderWriterLock: Lock that supports single writer and multiple readers. If you have a scenario where you read your data frequently but update only once in a while, consider using this as it supports multiple readers.
  d.e. ReaderWriterLockSlim: Similar to ReaderWriterLock but simplified rules for recursion and for upgrading and downgrading lock state. It also avoids many cases of potential deadlock and has improved performance. Using this is recommended.
  d.f. Semaphore: Limits # of threads that can access a resource or pool of resources concurrently. Use it only when you need to control pool of resources.


e) Never use Thread.Suspend and Thread.Resume to synchronize activities. The suspend and resume operation doesn’t happen immediately as CLR has to make sure the execution control is in safe point. This can lead to race conditions or deadlock (1)

f) Never use Thread.Abort to abort another thread: (1)

The Entire Game Should Move Onto the GPU, says Rev Lebaredian   As the computing functionality and horsepower of GPUs has grown over the last few years, the role of the GPU is rapidly expanding to game tasks beyond rasterization-based graphics. Now...
The Entire Game Should Move Onto the GPU, says Rev Lebaredian   As the computing functionality and horsepower of GPUs has grown over the last few years, the role of the GPU is rapidly expanding to game tasks beyond rasterization-based graphics. Now...
SOA with SQL Server   A key component to any distributed architecture built with SOA methodologies in mind is the database infrastructure. Service Oriented Database Architecture (SODA) is much easier to...
A Lap Around Visual Studio Team System   Wonder why 70% of projects are classified as failed projects? Working in silos does not help projects succeed. Learn how Visual Studio Team System 2008 helps break the walls across team...
Busting Common Myths about Developer Productivity   Many traditional assumptions about software development have been challenged in recent years. Agile processes and service oriented architectures are two examples of this. An area that has...
.NET Gotchas Workshop by Venkat Subramaniam: Part III   Programmers working on the .NET framework know the power and increased productivity that comes with it, says Dr. Venkat Submramaniam. Like any development, however, there are things that...
Silverlight Deep Dive Workshop by Todd Anglin: Part III   Do you want to truly understand Silverlight? If so, do not miss this three-hour workshop that will cover everything from Silverlight basics to advanced topics like cross-site XHR. In the...
.NET Gotchas Workshop: Part II   There are several things that one should pay attention to while programming on .NET, says Dr. Venkat Submramaniam. Are there things in .NET that, if we do not pay attention to, may result...
To:
Name:*
E-mail address:*
Your Details:
Your name: *
E-mail address: *
Message:
Software Supportby Advanced Millennium Technologies

Advanced Millennium Technologies. Expertise in software development, offering consultancy services, Open source programming, CRM - Customer Relationship Management, CMS - Content Management System , ERP - Enterprise Resource Planning and Ecommerce development, AJAX, PHP, .NET, J2EE, SOA, XSLT, DOJO toolkit development and software testing. A robust onsite-offshore model. A well-defined global delivery model. AMT Outsourcing center. www.amt.inTAROBY - The E-Mail Dashboard for EntrepreneursTaroby is a SaaS based messaging and collaboration suite inbox that enables sharing of email accounts among team members. The unique concept of 'Team Inbox' makes Taroby an excellent enterprise collaboration suite for enterprises. Taroby is an effective tool for CEO's and entrepreneurs to manage multiple departments or manage multiple projects under them. The team inbox gives the entrepreneurs an overview of what is happening their business and give a quick snap shot of the employees who is responcible for handling the tasks/emails. For team members taroby brings in transparency and efficiency in their teams. Taroby improves the internal and external communication in an organization. Using the Taroby's Team Inbox also helps in reducing the usage of disc space and there by helping the enterprises to reduce carbon footprints.