What is the difference between locking on a type of a class vs locking on the class itself?
For example:
private readonly object xmpp = new object();
lock (xmpp)
{
...
}
vs
lock (typeof(Xmpp))
{
...
}
lock(x)
synchronizes on a different lock for each instance of the type
lock(typeof(X))
synchronizes on the same lock for all instances of the type
Always lock on a private lock object:
public class X
{
private readonly Object _lock = new Object();
// ...
lock (_lock)
{
}
If you must synchronize access to class static members, use the same pattern:
public class X
{
private readonly static Object s_lock = new Object();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With