How to get Synchronize access to the Stack in C#?

In C#, the Stack class is not thread-safe by default. To achieve synchronized access to a Stack in a multithreaded environment, you can use the SyncRoot property with the lock statement. This ensures that only one thread can access the Stack at a time, preventing data corruption and race conditions.

Syntax

Following is the syntax for synchronizing access to a Stack −

lock(stack.SyncRoot) {
   // Thread-safe operations on stack
   foreach(Object item in stack) {
      // Process items safely
   }
}

How It Works

The SyncRoot property returns an object that can be used to synchronize access to the Stack. When you use lock(stack.SyncRoot), it ensures that:

  • Only one thread can execute the code inside the lock block at a time.

  • Other threads must wait until the lock is released before accessing the Stack.

  • All Stack operations within the lock are atomic and thread-safe.

Thread Synchronization with SyncRoot Thread 1 Has Lock ? Accessing Stack Thread 2 Waiting ? Blocked Thread 3 Waiting ? Blocked Stack (SyncRoot) Thread-Safe Access

Using Synchronized Access with Integer Stack

Example

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      Stack stack = new Stack();
      stack.Push(100);
      stack.Push(200);
      stack.Push(300);
      stack.Push(400);
      stack.Push(500);
      
      Console.WriteLine("Stack...");
      foreach(Object ob in stack) {
         Console.WriteLine(ob);
      }
      Console.WriteLine("Count of elements = " + stack.Count);
      
      Console.WriteLine("Synchronize access...");
      lock(stack.SyncRoot) {
         foreach(Object ob in stack) {
            Console.WriteLine(ob);
         }
      }
   }
}

The output of the above code is −

Stack...
500
400
300
200
100
Count of elements = 5
Synchronize access...
500
400
300
200
100

Using Synchronized Access with String Stack

Example

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      Stack stack = new Stack();
      stack.Push("Jacob");
      stack.Push("Tim");
      stack.Push("Philips");
      stack.Push("Tom");
      stack.Push("Amy");
      stack.Push("Katie");
      stack.Push("Selena");
      stack.Push("Taylor");
      stack.Push("Justin");
      
      Console.WriteLine("Stack...");
      foreach(Object ob in stack) {
         Console.WriteLine(ob);
      }
      
      Console.WriteLine("\nSynchronize access...");
      lock(stack.SyncRoot) {
         foreach(Object ob in stack) {
            Console.WriteLine(ob);
         }
      }
   }
}

The output of the above code is −

Stack...
Justin
Taylor
Selena
Katie
Amy
Tom
Philips
Tim
Jacob

Synchronize access...
Justin
Taylor
Selena
Katie
Amy
Tom
Philips
Tim
Jacob

Modern Alternatives

While SyncRoot works for the legacy Stack class, modern C# applications should consider using ConcurrentStack<T> from the System.Collections.Concurrent namespace, which provides built-in thread safety without explicit locking.

Conclusion

Synchronized access to Stack in C# is achieved using the SyncRoot property with the lock statement. This ensures thread-safe operations by allowing only one thread to access the Stack at a time, preventing race conditions in multithreaded applications.

Updated on: 2026-03-17T07:04:36+05:30

167 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements