多线程和锁定(线程安全的操作) [英] Multithreading and Locking (Thread-Safe operations)

查看:150
本文介绍了多线程和锁定(线程安全的操作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我必须与所有使用以防止奇怪的事情发生锁定了几个方法的类当有人使用我的多线程访问它的类的实例:

So I have a class with a few methods which all use locking in order to prevent weird things happening when someone uses an instance of my class with multiple threads accessing it:

public class SomeRandomClass
{
    private object locker = new object();

    public void MethodA()
    {
        lock (locker)
        {
            // Does something
            MethodB();
        }
    }

    public void MethodB()
    {
        lock (locker)
        {
            // Does something else
        }
    }
}

我们可以看到,的methodB()将自动治法(),但不会工作,因为治法()目前已经锁定了更衣室对象。

As we can see, MethodB() is automatically accessed by MethodA(), but that won't work since MethodA() has currently locked the locker object.

我要让的methodB()公共访问,这样你就可以手动调用它需要的时候,但我不希望它被使用,而治法()正在做的事情(这就是为什么我使用的是更衣室对象)。

I want to make MethodB() accessible publicly, so you can call it manually whenever needed, but I do NOT want it to be used while MethodA() is doing things (that's why I'm using a locker object).

当然,我不希望治法()做的事情,而的methodB()正在做的东西。我基本上只希望在同一时间内使用的所有方法之一,但治法()需要访问的methodB()不知何故而不删除锁(使其保持完全线程安全的全部时间)。

And of course I do not want MethodA() to do things while MethodB() is doing stuff. I basically want only one of all the methods to be used at the same time, but MethodA() needs to access MethodB() somehow without removing the lock (so that it stays completely thread-safe the whole time).

我真的希望这是一种可以理解的是我想要问...如果有关于我的问题有任何疑问,那么请继续下面和张贴。答案/解决方案都非常感激的!

I really hope it is kind of understandable what I'm trying to ask... If there's any questions about my question, then please go ahead and post them below. Answers/Solutions are very much appreciated as well!

解决方案可能是非常容易的,我只是没有看到它。

The solution is probably incredibly easy and I'm just not seeing it.

顺便提一下,上面应该是C#-code。

By the way, the above is supposed to be C#-code.

推荐答案

这是简单的解决办法是将创建一个私人方式包含什么方法b 也可以通过治法叫做和另一公开 方法b

An easy solution would be to create a private method that contains what MethodB does that can be called by MethodA and another public MethodB

私人 <$ C $ 。C>方法b 不锁,只有公共的人做

The private MethodB does not lock, only the public ones do.

例如:

public class SomeRandomClass {

    private object locker = new object();

    public void MethodA {
        lock(locker) {
            // exclusive club
            // do something before calling _methodB
            _methodB();
        }
    }
    private void _methodB {
        // do that, what used to be done by MethodB
    }
    public void MethodB {
        //this one only exists to expose _methodB in a thread-safe context
        lock(locker) {      
            _methodB();
        }
    }
}






PS


P.S.

我认为这是显而易见的你和其他人为什么你的代码是有点的设计的创建死锁。

I think it is obvious to you and everyone else why your code is somewhat designed to create a deadlock.

更新

显然, 锁(对象){} 重入在评论中指出,如此明显的僵局是连一个也没有。

Apparently lock(object) {} is re-entrant as pointed out in the comments, so the obvious deadlock isn't even one.

这篇关于多线程和锁定(线程安全的操作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆