Click here to Skip to main content
14,487,848 members

Writing Thread Safe Code in C#

Rate this:
3.90 (22 votes)
Please Sign up or sign in to vote.
3.90 (22 votes)
9 Jul 2009CPOL
This article describe how to write thread safe code for multithreading.


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 = "" },
            new Server{ Name = "Server2", IPAddress = "" },
            new Server{ Name = "Server3", IPAddress = "" },
            new Server{ Name = "Server4", IPAddress = "" }, 

    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
            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) ); 

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

        // loop forever
        while( true )
                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.
    static void Main(string[] args)
        clsMain cm = new clsMain();

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

By locking the instance of MythreadClass, MythreadClass gets deadlocked.


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


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 5 Pin
Member 1033374711-Nov-15 2:31
MemberMember 1033374711-Nov-15 2:31 
GeneralMy vote of 1 Pin
Thomas Croux PTC19-Jan-11 23:43
MemberThomas Croux PTC19-Jan-11 23:43 
GeneralMy vote of 1 Pin
Roman Kotovich30-Jul-09 22:41
MemberRoman Kotovich30-Jul-09 22:41 
GeneralFurther explanations regarding the chances of deadlock Pin
Andrei Ion Rînea13-Jul-09 22:49
MemberAndrei Ion Rînea13-Jul-09 22:49 
Ranttypo : synchronise Pin
Andrei Ion Rînea13-Jul-09 22:44
MemberAndrei Ion Rînea13-Jul-09 22:44 
Rantlock is not a second option but syntactic sugar Pin
Andrei Ion Rînea13-Jul-09 22:43
MemberAndrei Ion Rînea13-Jul-09 22:43 
GeneralMy vote of 1 Pin
h3211-Jul-09 6:40
Memberh3211-Jul-09 6:40 
GeneralThree major points you should mention Pin
supercat910-Jul-09 7:15
Membersupercat910-Jul-09 7:15 
GeneralMy vote of 2 Pin
Member 617960410-Jul-09 0:34
MemberMember 617960410-Jul-09 0:34 
GeneralMy vote of 2 Pin
Artem S. Dmitriev9-Jul-09 9:39
MemberArtem S. Dmitriev9-Jul-09 9:39 
poor artcle
GeneralNice! Pin
sgorozco9-Jul-09 5:48
Membersgorozco9-Jul-09 5:48 
GeneralSimple Assignments Pin
CoolDadTx9-Jul-09 3:36
MemberCoolDadTx9-Jul-09 3:36 
Generalnew Singleton instace. Pin
Paulo Zemek9-Jul-09 3:17
professionalPaulo Zemek9-Jul-09 3:17 
GeneralRe: new Singleton instace. Pin
Superstom15-Jul-09 11:22
MemberSuperstom15-Jul-09 11:22 

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

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

Posted 9 Jul 2009


42 bookmarked