C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > c# 共享资源访问

c# 多线程环境下控制对共享资源访问的解决方法

作者:我只吃饭不洗碗

这篇文章主要介绍了c# 多线程环境下控制对共享资源访问的解决方法,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧

c# 多线程环境下控制对共享资源访问的办法

  class Program
{
    static readonly object _lock = new object();
    static int _counter = 0;
    static void Main()
    {
        for (int i = 0; i < 10; i++)
        {
            new Thread(IncrementCounter).Start();
        }
        Console.ReadKey();
    }
    static void IncrementCounter()
    {
        Monitor.Enter(_lock);
        try
        {
            _counter++;
            Console.WriteLine($"Counter: {_counter}");
        }
        finally
        {
            Monitor.Exit(_lock);
        }
    }
}
Monitor
  class Program
{
    static Mutex _mutex = new Mutex();
    static int _counter = 0;
    static void Main()
    {
        for (int i = 0; i < 10; i++)
        {
            new Thread(IncrementCounter).Start();
        }
        Console.ReadKey();
    }
    static void IncrementCounter()
    {
        _mutex.WaitOne();
        _counter++;
        Console.WriteLine($"Counter: {_counter}");
        _mutex.ReleaseMutex();
    }
}
Mutex
class Program
{
    static ReaderWriterLockSlim _rwLock = new ReaderWriterLockSlim();
    static int _counter = 0;
    static void Main()
    {
        for (int i = 0; i < 5; i++)
        {
            new Thread(ReadCounter).Start();
            new Thread(IncrementCounter).Start();
        }
        Console.ReadKey();
    }
    static void ReadCounter()
    {
        _rwLock.EnterReadLock();
        Console.WriteLine($"Counter: {_counter}");
        _rwLock.ExitReadLock();
    }
    static void IncrementCounter()
    {
        _rwLock.EnterWriteLock();
        _counter++;
        Console.WriteLine($"Counter incremented to: {_counter}");
        _rwLock.ExitWriteLock();
    }
}
ReaderWriterLockSlim
  class Program
{
    static Semaphore _semaphore = new Semaphore(2, 2); // Allow 2 threads to access the resource simultaneously
    static int _counter = 0;
    static void Main()
    {
        for (int i = 0; i < 5; i++)
        {
            new Thread(IncrementCounter).Start();
        }
        Console.ReadKey();
    }
    static void IncrementCounter()
    {
        _semaphore.WaitOne();
        _counter++;
        Console.WriteLine($"Counter: {_counter}");
        _semaphore.Release();
    }
}
Semaphore
class Program
{
    static SemaphoreSlim _semaphore = new SemaphoreSlim(2, 2); // Allow 2 threads to access the resource simultaneously
    static int _counter = 0;
    static void Main()
    {
        for (int i = 0; i < 5; i++)
        {
            new Thread(IncrementCounter).Start();
        }
        Console.ReadKey();
    }
    static void IncrementCounter()
    {
        _semaphore.Wait();
        _counter++;
        Console.WriteLine($"Counter: {_counter}");
        _semaphore.Release();
    }
}
SemaphoreSlim
class Program
{
    static readonly object _lock = new object();
    static int _counter = 0;
    static void Main()
    {
        for (int i = 0; i < 5; i++)
        {
            new Thread(IncrementCounter).Start();
        }
        Console.ReadKey();
    }
    static void IncrementCounter()
    {
        lock (_lock)
        {
            _counter++;
            Console.WriteLine($"Counter: {_counter}");
        }
    }
}
lock

到此这篇关于c# 多线程环境下控制对共享资源访问的办法的文章就介绍到这了,更多相关c# 共享资源访问内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文