DEV Community

Cover image for ✅ 𝗧𝗵𝗿𝗲𝗮𝗱 𝗦𝘆𝗻𝗰𝗵𝗿𝗼𝗻𝗶𝘇𝗮𝘁𝗶𝗼𝗻 𝗦𝗶𝗺𝗽𝗹𝗶𝗳𝗶𝗲𝗱 𝗶𝗻 .𝗡𝗘𝗧 𝟵: 𝗨𝗻𝗱𝗲𝗿𝘀𝘁𝗮𝗻𝗱𝗶𝗻𝗴 𝗟𝗼𝗰𝗸 𝗠𝗲𝘁𝗵𝗼𝗱𝘀
Apurv Upadhyay
Apurv Upadhyay

Posted on

✅ 𝗧𝗵𝗿𝗲𝗮𝗱 𝗦𝘆𝗻𝗰𝗵𝗿𝗼𝗻𝗶𝘇𝗮𝘁𝗶𝗼𝗻 𝗦𝗶𝗺𝗽𝗹𝗶𝗳𝗶𝗲𝗱 𝗶𝗻 .𝗡𝗘𝗧 𝟵: 𝗨𝗻𝗱𝗲𝗿𝘀𝘁𝗮𝗻𝗱𝗶𝗻𝗴 𝗟𝗼𝗰𝗸 𝗠𝗲𝘁𝗵𝗼𝗱𝘀

In 𝗺𝘂𝗹𝘁𝗶𝘁𝗵𝗿𝗲𝗮𝗱𝗲𝗱 𝗽𝗿𝗼𝗴𝗿𝗮𝗺𝗺𝗶𝗻𝗴, ensuring 𝗱𝗮𝘁𝗮 𝗰𝗼𝗻𝘀𝗶𝘀𝘁𝗲𝗻𝗰𝘆 and avoiding 𝗿𝗮𝗰𝗲 𝗰𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻𝘀 is critical. .NET 9 introduces enhancements to make t𝗵𝗿𝗲𝗮𝗱 𝘀𝘆𝗻𝗰𝗵𝗿𝗼𝗻𝗶𝘇𝗮𝘁𝗶𝗼𝗻 easier and more efficient. Let’s break it down!

Image description

🔒 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗧𝗵𝗿𝗲𝗮𝗱 𝗦𝘆𝗻𝗰𝗵𝗿𝗼𝗻𝗶𝘇𝗮𝘁𝗶𝗼𝗻?

When multiple threads access shared resources, synchronization ensures 𝘀𝗮𝗳𝗲 𝗲𝘅𝗲𝗰𝘂𝘁𝗶𝗼𝗻 by controlling their access to these resources. Without proper synchronization, you risk data corruption and unpredictable behavior.

🌟 𝗡𝗲𝘄 𝗶𝗻 .𝗡𝗘𝗧 𝟵: 𝗟𝗼𝗰𝗸 𝗦𝗶𝗺𝗽𝗹𝗶𝗳𝗶𝗰𝗮𝘁𝗶𝗼𝗻𝘀

1️⃣ 𝗟𝗼𝗰𝗸 𝘄𝗶𝘁𝗵 𝗧𝗶𝗺𝗲𝗼𝘂𝘁

Avoid indefinite waits by specifying a timeout duration.

2️⃣ 𝗥𝗲𝗮𝗱𝗲𝗿-𝗪𝗿𝗶𝘁𝗲𝗿 𝗟𝗼𝗰𝗸 𝗘𝗻𝗵𝗮𝗻𝗰𝗲𝗺𝗲𝗻𝘁𝘀

Ideal for scenarios with multiple readers and few writers, providing better performance and memory efficiency.

🎯 𝗪𝗵𝗲𝗻 𝘁𝗼 𝗨𝘀𝗲 𝗟𝗼𝗰𝗸𝘀?

🔸 𝗨𝗽𝗱𝗮𝘁𝗶𝗻𝗴 𝗦𝗵𝗮𝗿𝗲𝗱 𝗥𝗲𝘀𝗼𝘂𝗿𝗰𝗲𝘀: Protects shared data in a multithreaded environment.

🔸 𝗧𝗵𝗿𝗲𝗮𝗱-𝗦𝗮𝗳𝗲 𝗖𝗮𝗰𝗵𝗶𝗻𝗴: Ensures consistent state in memory cache.

🔸 𝗖𝗿𝗶𝘁𝗶𝗰𝗮𝗹 𝗢𝗽𝗲𝗿𝗮𝘁𝗶𝗼𝗻𝘀: Safeguards operations that must execute sequentially.
Enter fullscreen mode Exit fullscreen mode

🛠 𝗞𝗲𝘆 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆𝘀

🔹 Use lock for simplicity, but beware of deadlocks.

🔹 Use Monitor.TryEnter to avoid indefinite blocking.

🔹 Opt for 𝗥𝗲𝗮𝗱𝗲𝗿𝗪𝗿𝗶𝘁𝗲𝗿𝗟𝗼𝗰𝗸𝗦𝗹𝗶𝗺 for complex read-write scenarios.

🔹 Synchronize only 𝗰𝗿𝗶𝘁𝗶𝗰𝗮𝗹 𝘀𝗲𝗰𝘁𝗶𝗼𝗻𝘀 to minimize performance impact.
Enter fullscreen mode Exit fullscreen mode

𝗖𝗼𝗱𝗲 𝗦𝗼𝗹𝘂𝘁𝗶𝗼𝗻

using System;
using System.Threading;

class ThreadSynchronizationExample
{
    private readonly object _lock = new object();
    private int _sharedCounter = 0;

    public void IncrementCounter()
    {
        // Basic lock example
        lock (_lock)
        {
            _sharedCounter++;
            Console.WriteLine($"Counter incremented: {_sharedCounter}");
        }
    }

    public void PerformTaskWithTimeout()
    {
        // Lock with timeout
        if (Monitor.TryEnter(_lock, TimeSpan.FromSeconds(2)))
        {
            try
            {
                _sharedCounter++;
                Console.WriteLine($"Counter incremented with timeout: {_sharedCounter}");
            }
            finally
            {
                Monitor.Exit(_lock);
            }
        }
        else
        {
            Console.WriteLine("Failed to acquire lock within the timeout.");
        }
    }

    public void ReaderWriterLockExample()
    {
        ReaderWriterLockSlim rwLock = new ReaderWriterLockSlim();

        // Writer lock
        rwLock.EnterWriteLock();
        try
        {
            _sharedCounter++;
            Console.WriteLine($"Writer incremented counter: {_sharedCounter}");
        }
        finally
        {
            rwLock.ExitWriteLock();
        }

        // Reader lock
        rwLock.EnterReadLock();
        try
        {
            Console.WriteLine($"Reader accessed counter: {_sharedCounter}");
        }
        finally
        {
            rwLock.ExitReadLock();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Thread synchronization in .𝗡𝗘𝗧 𝟵 is 𝘀𝗶𝗺𝗽𝗹𝗲𝗿, 𝗳𝗮𝘀𝘁𝗲𝗿, 𝗮𝗻𝗱 𝘀𝗮𝗳𝗲𝗿 than ever, empowering developers to write efficient and scalable multithreaded applications.

Please 𝗿𝗲𝗽𝗼𝘀𝘁 ♻ to spread the knowledge if you find it useful 🔔 Follow Apurv Upadhyay ☁️ for more insightful content like this!

DotNet9 #Multithreading #ThreadSafety #Synchronization #LockMethods #CSharp

Top comments (0)