How to get Synchronize access to an Array in C#?

To get synchronized access to an array in C#, you can use the SyncRoot property of the array along with the lock statement. This ensures thread-safe access when multiple threads are accessing the same array concurrently.

The SyncRoot property returns an object that can be used to synchronize access to the array, preventing data races and ensuring thread safety in multi-threaded applications.

Syntax

Following is the syntax for synchronizing access to an array −

lock(array.SyncRoot) {
   // Thread-safe operations on the array
   foreach (var item in array) {
      // Process each item safely
   }
}

How It Works

The lock statement ensures that only one thread can execute the code block at a time. When a thread enters the lock block, other threads must wait until the first thread exits the block before they can proceed.

Thread Synchronization with SyncRoot Thread 1 Active in lock Thread 2 Waiting for lock Thread 3 Waiting for lock Array.SyncRoot Only one thread can access the array at a time

Using SyncRoot with Integer Array

Example

using System;

public class Demo {
   public static void Main() {
      Array intArr = new int[] {5, 10, 15, 20, 25, 30, 35, 40 };
      Console.WriteLine("Integer array...");
      foreach (int i in intArr)
         Console.WriteLine(i);
      Console.WriteLine("After applying lock on array...");
      lock(intArr.SyncRoot) {
         foreach (Object ob in intArr)
            Console.WriteLine(ob);
      }
   }
}

The output of the above code is −

Integer array...
5
10
15
20
25
30
35
40
After applying lock on array...
5
10
15
20
25
30
35
40

Using SyncRoot with String Array

Example

using System;

public class Demo {
   public static void Main() {
      Array strArr = new String[] {"Harry", "Tom", "Kevin", "Ryan", "Katie", "Amy" };
      Console.WriteLine("String array...");
      foreach (string i in strArr)
         Console.WriteLine(i);
      Console.WriteLine("After applying lock on array...");
      lock(strArr.SyncRoot) {
         foreach (Object ob in strArr)
            Console.WriteLine(ob);
      }
   }
}

The output of the above code is −

String array...
Harry
Tom
Kevin
Ryan
Katie
Amy
After applying lock on array...
Harry
Tom
Kevin
Ryan
Katie
Amy

Practical Multi-Threading Example

Example

using System;
using System.Threading;

public class ThreadSafeArrayDemo {
   static Array sharedArray = new int[] {1, 2, 3, 4, 5};
   
   public static void Main() {
      Thread t1 = new Thread(AccessArray);
      Thread t2 = new Thread(AccessArray);
      
      t1.Start();
      t2.Start();
      
      t1.Join();
      t2.Join();
      
      Console.WriteLine("All threads completed safely.");
   }
   
   static void AccessArray() {
      lock(sharedArray.SyncRoot) {
         Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId} accessing array:");
         foreach (int value in sharedArray) {
            Console.WriteLine($"  Value: {value}");
            Thread.Sleep(100); // Simulate work
         }
      }
   }
}

The output of the above code is −

Thread 1 accessing array:
  Value: 1
  Value: 2
  Value: 3
  Value: 4
  Value: 5
Thread 4 accessing array:
  Value: 1
  Value: 2
  Value: 3
  Value: 4
  Value: 5
All threads completed safely.

Key Points

  • SyncRoot provides a synchronization object for thread-safe access to arrays.

  • The lock statement ensures only one thread can access the array at a time.

  • This approach prevents race conditions and data corruption in multi-threaded scenarios.

  • All collection types in .NET implement the SyncRoot property for synchronization.

Conclusion

Synchronizing access to arrays using SyncRoot and the lock statement ensures thread safety in multi-threaded applications. This prevents race conditions and guarantees that only one thread can access the array at any given time, maintaining data integrity.

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

206 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements