当前位置: 代码网 > it编程>编程语言>C# > c# 多线程环境下控制对共享资源访问的解决方法

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

2024年09月08日 C# 我要评论
c# 多线程环境下控制对共享资源访问的办法monitor:定义:monitor是 c# 中最基本的同步机制,通过enter和exit方法来控制对共享资源的访问。它提供了排他锁的功能,确保在任何时刻只有

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

  • monitor:
    • 定义:monitor 是 c# 中最基本的同步机制,通过 enter 和 exit 方法来控制对共享资源的访问。它提供了排他锁的功能,确保在任何时刻只有一个线程可以访问共享资源。
    • 优点:简单易用,适合对临界区进行粗粒度的同步控制。
    • 缺点:只能实现排它锁,不能实现读写锁,性能相对较低。
  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
  • mutex:
    • 定义:mutex 是一个操作系统对象,用于在进程间共享,通过 waitone 和 releasemutex 来控制对共享资源的访问。它提供了进程间的同步能力。
    • 优点:可跨进程使用,适合在进程间进行同步。
    • 缺点:相比 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
  • readerwriterlockslim:
    • 定义:readerwriterlockslim 实现了读写分离锁,允许多个线程同时读取共享资源,但只允许一个线程写入共享资源。这种机制适用于读多写少的场景。
    • 优点:适合读多写少的场景,提高了并发性能。
    • 缺点:相对复杂,可能会引起死锁,不支持递归锁。
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
  • semaphore:
    • 定义:semaphore 是一个信号量,用于控制同时访问共享资源的线程数量。通过 waitone 和 release 方法,可以控制访问资源的线程数量。
    • 优点:可以控制多个线程同时访问共享资源的数量,灵活性较高。
    • 缺点:相对于其他机制,使用起来较为复杂,需要谨慎处理信号量的释放。
  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
  • semaphoreslim:
    • 定义:semaphoreslim 是轻量级的信号量,与 semaphore 类似,用于控制同时访问共享资源的线程数量,但相比 semaphore 更轻量级。
    • 优点:相比 semaphoresemaphoreslim 的开销更小,适用于资源访问频繁的场景。
    • 缺点:与 semaphore 相比,功能上略有限制,例如没有 release(int32) 方法,只能递增信号量一个单位。
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
  • lock:
    • 定义:lock 是 c# 中的关键字,用于在代码块级别实现互斥锁,保护共享资源不被多个线程同时访问。
    • 优点:简单易用,适合对临界区进行细粒度的同步控制,编写起来比较方便。
    • 缺点:只能用于单线程内部的同步,不能跨越线程或进程,如果不小心使用会导致死锁。
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# 共享资源访问内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com