Best practice for having more than a lock object in a class

In the class below, I have methods that do two completely different things that have nothing to do with eachother. But I use only one lock object in both of them.

My question is: what is the best practice in such scenarios? Having two separate lock objects for each or separating them (how do we do it)?

class MyClass
{

private static object _lock = new object();

public void DoSomething()
{
    lock (_lock)
    {

    }
}

public void DoSomethingTotallyDifferent()
{
    lock (_lock)
    {

    }
}
}
+4
source share
2 answers

The question arises: is it possible DoSomethingor possible to DoSomethingTotallyDifferentcall from each other? If so, you can create a dead end. Maybe now he does not call each other, but maybe in the future.

: , . .

+4

, .

, A DoSomething, _lockA. DoSomethingTotallyDifferent , . B, , , _lockB. , ...

+5

All Articles