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)