PostSharp4.3//Threading Patterns/Writing Thread-Safe Code with Threading Models/Synchronized Threading Model
Synchronized Threading Model

A common way to avoid data races is to enclose all public instance methods of a class with a lock(this) statement. This is basically what the Synchronized model does

This article describes how to use the Synchronized model and how it differs from the use of lock(this) statement.

This topic contains the following sections:

Comparing with lock(this)

Traditionally, the C# keyword lock(this) statement has been used to synchronize access of several threads to a single object. When an object is locked by one thread, any other object that attempts to access that object will have its execution blocked.

private object myLockingObject = new Object();

public void DoSomething()
        //some code that does something in one thread at a time

In this example the myLockingObject member variable is used as a locking object. Once a thread runs the lock(myLockingObject) line, all other threads that enter the DoSomething method will stop executing, or be blocked, until the original thread has exited the lock(myLockingObject) code block.

The Synchronized model is similar to using the lock statement around every single public method, but it has the following differences:

  • It is not technically equivalent to locking the current instance (this). Another object is actually being locked.

  • Locking is automatic for all public and internal instance methods. You cannot forget it.

  • If a thread attempts to access a field without having first acquired access to the object (by invoking a public or internal method), an exception will be thrown.

  • The pattern also works with entities composed of several objects organized in a tree.

Applying the Synchronized Model using PostSharp Tools for Visual Studio

To apply the Synchronized threading model to your class with PostSharp Tools for Visual Studio:

  1. Place the cursor over your class name and select "Apply threading model..." from the drop down

    Synchronized 1
  2. Select "Apply Synchronized threading model" from the Select Model tab and select "Next".

    Synchronized 2
  3. Confirm the actions on the Summary tab and select "Next".

    Synchronized 3
  4. Click Finish after the installation completes.

    Synchronized 4

    Your class will now have the SynchronizedAttribute and any previously missing PostSharp related references will have been added to the project.

Applying Synchronized manually

To apply the Synchronized threading model manually:

  1. Add the PostSharp.Patterns.Threading NuGet package to your project.

  2. Add the SynchronizedAttribute to the class.

In the example below the SynchronizedAttribute has been added to the class.

public class OrderService
    public void Process(int sequence)
        Console.WriteLine("sequence {0}", sequence);
        Console.WriteLine("sleeping for 10s");

        Thread.Sleep(new TimeSpan(0,0,10));

To test this we can run the following code.

public void Main()
    var orderService = new OrderService();

    var backgroundWorker = new BackgroundWorker();
    backgroundWorker.DoWork += (sender, args) => orderService.Process(1);


The code above will attempt to execute the Process method on two different threads; the main thread and a background worker thread. Because these two threads are trying to access the same instance of the OrderService the first thread to access it will block the second. As a result, when you run the program you will first see the follwing.

Synchronized 5

Because the OrderService.Process method has a Thread.Sleep call, the first thread accessing that method will block the second for 10 seconds. After those 10 seconds have passed the second thread will no longer be blocked and it will be able to continue its execution.

Synchronized 6
Working with object trees

Because the Synchronized model is an implementation of the Aggregatable pattern, all of the same behaviors of the AggregatableAttribute are available. For more information regarding object trees, read Parent/Child Relationships.

Note Note

Once you have established your parent-child relationships you will need to apply compatible threading models to the child classes. You will want to refer to the Compatibility of Threading Models article to determine which threading model will work for the children of the Synchronized object.

See Also