Click here to Skip to main content
Click here to Skip to main content

Writing Thread Safe Code in C#

, 9 Jul 2009
Rate this:
Please Sign up or sign in to vote.
This article describe how to write thread safe code for multithreading.

Introduction

In multi-threaded applications where multiple threads make calls to the methods of a single object, it is necessary that those calls be synchronized. If code is not synchronized, then one thread might interrupt another thread and the object could be left in an invalid state. A class whose members are protected from such interruptions is called thread-safe.

Ways to implement

In the .NET Framework, there are many primitives available that you can use to make synchronized calls to shared data. One of them is the Monitor class. The Monitor.Enter() and Monitor.Exit() methods implement a critical section block.

Use the Enter and Exit methods to mark the beginning and end of a critical section. If the critical section is a set of contiguous instructions, then the lock acquired by the Enter method guarantees that only a single thread can execute the enclosed code with the locked object. In this case, it is recommended you place those instructions in a try block and place the Exit instruction in a finally block. This facility is typically used to synchronize access to a static or instance method of a class. If an instance method requires synchronized thread access, it invokes the Enter and corresponding Exit methods using the current instance as the object to lock. Since only one thread can hold the lock on the current instance, the method can only be executed by one thread at a time. Static methods are protected in a similar fashion using the type of the current instance as the locked object.

If a critical section spans an entire method, the locking facility described above can be achieved by placing System.Runtime.CompilerServices..::.MethodImplAttribute on the method, and specifying the synchronized value in the constructor of MethodImplAttribute. Using this attribute, the Enter and Exit statements are not needed.

A second option is a lock statement to implement a critical section.

There are certain conditions where a Monitor gives you the necessary control. The Monitor class provides the Wait, Pulse, and PulseAll methods to implement timeout, signaling, and a consumer/producer design. We can't achieved these by implementing a lock statement.

Using the code

ServerLoadBalancerEngine implements the Singleton pattern and here we need to ensure that multiple thread calls to GetServerLoadBalancerEngine should be synchronized.

The static object syncLock is used to implement a critical section in the GetServerLoadBalancerEngine function.

To synchronise static methods, we need to lock the private static object variable. Locking GetServerLoadBalancerEngine ensures that only one thread can operate on this method, so only one instance will be created of the ServerLoadBalancerEngine class.

sealed class ServerLoadBalancerEngine
{
    private static ServerLoadBalancerEngine _instance;
    private List<Server> _servers;
    private Random _random = new Random();

    // syncLock object, used to lock the code block
    private static object syncLock = new object();

    // constructor is 'private'
    private ServerLoadBalancerEngine()
    {

        // Load list of available servers
        _servers = new List<Server>
        {
            new Server{ Name = "Server1", IPAddress = "120.14.220.18" },
            new Server{ Name = "Server2", IPAddress = "120.14.220.19" },
            new Server{ Name = "Server3", IPAddress = "120.14.220.20" },
            new Server{ Name = "Server4", IPAddress = "120.14.220.21" }, 
        };
    }

    public static ServerLoadBalancerEngine GetServerLoadBalancerEngine()
    {
        if (_instance == null)
        {
            //To ensure thread safety
            lock (syncLock)
            {
                if (_instance == null)
                {
                    _instance = new ServerLoadBalancerEngine();
                }
            }
        }
        return _instance;
    }

    // Simple, but effective load balancer
    public Server NextServer
    {
        get
        {
            int r = _random.Next(_servers.Count);
            return _servers[r];
        }
    }
}

Deadlock conditions when implementing a lock statement

Never lock the type that is GetType(MyType) and the instance (this), by doing so there are chances of a deadlock. To lock a static method, use a private static object, and for a private object, lock the instance method.

When locking a class instance, this will work fine if it is exposed externally and used.

Suppose I have written MythreadClass and locking this to implement a thread safety.

class MythreadClass
{
    Thread t = null;
    public MythreadClass()
    {
        t = new Thread( new ThreadStart( PrintFunction) ); 
        t.Start();
    }

    /// Internal Thread Function
    private void PrintFunction()
    {
        Thread.Sleep( 10000 );
        Console.WriteLine( "PrintFunction Startup " + 
                "Complete trying to call lock(this)" );

        // loop forever
        while( true )
        {
            lock(this)
            {
                Console.WriteLine( "Running Internal ThreadFunction " + 
                                   "- Starting(takes 4 seconds)" );

                // do some work
                Thread.Sleep( 3000 );
                Console.WriteLine( "Running Internal PrintFunction- Complete" );
            }
        }
    }
}

Now, this class is instantiated externally, and here is how the lock is implemented:.

class clsMain
{
    ///MythreadClass class Object
    private MythreadClass theClass = MythreadClass ();

    /// 
    /// 
    public ClassMain()
    {
        Console.WriteLine( "Locking MythreadClass Class " + 
                "before Internal ThreadFunction does " );
        lock( theClass )
        {
            Console.WriteLine( "MythreadClass Class Locked - " + 
                "we have inadvertently deadlocked the Internal Class!" );

            // do some work
            Thread.Sleep( 30000 );
        }
        Console.WriteLine( "UnLocking MythreadClass Class - " + 
                "Internal Class will now Start Logging" );
    }

    /// 
    /// The main entry point for the application.
    /// 
    [STAThread]
    static void Main(string[] args)
    {
        clsMain cm = new clsMain();

        Console.WriteLine( "Press Return to exit" );
        Console.ReadLine();
    }
}

By locking the instance of MythreadClass, MythreadClass gets deadlocked.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Aneesur Rehman Khan
Team Leader
India India
Anees is working as Sr. Team Lead based in Delhi(India).He is Post graduated in Computer applications and science.
 
He is having around 11 years of design,analysis and coding experience in Sharepoint, ASP.NET, C#, VB.NET, SQL Server 2000/05, Reporting Services,Analysis Services,VB 6.0 and Crystal Reports.

Comments and Discussions

 
GeneralMy vote of 1 PinmemberThomas Croux PTC19-Jan-11 23:43 
GeneralMy vote of 1 PinmemberRoman Kotovich30-Jul-09 22:41 
GeneralFurther explanations regarding the chances of deadlock PinmemberAndrei Rinea13-Jul-09 22:49 
Ranttypo : synchronise PinmemberAndrei Rinea13-Jul-09 22:44 
Rantlock is not a second option but syntactic sugar PinmemberAndrei Rinea13-Jul-09 22:43 
GeneralMy vote of 1 Pinmemberh3211-Jul-09 6:40 
GeneralThree major points you should mention Pinmembersupercat910-Jul-09 7:15 
GeneralMy vote of 2 PinmemberMember 617960410-Jul-09 0:34 
GeneralMy vote of 2 Pinmembermartyn_mcfly9-Jul-09 9:39 
GeneralNice! Pinmembersgorozco9-Jul-09 5:48 
GeneralSimple Assignments PinmemberTaylorMichaelL9-Jul-09 3:36 
Generalnew Singleton instace. PinmemberPaulo Zemek9-Jul-09 3:17 
GeneralRe: new Singleton instace. PinmemberSuperstom15-Jul-09 11:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 9 Jul 2009
Article Copyright 2009 by Aneesur Rehman Khan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid