where developers meet development
Monday,November 18,2019

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)

Register to DeveloperMarch

Be part of DeveloperMarch and get updates on upcoming events. Youcan register with the form below:

FirstName*
LastName*
Company*
Country*
Job Title*
Phone Number*
Email*
UserName*
Password*
Confirm Password*
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.