1

Why would someone use a new mutex just for locking?

public class Job
{
    private static Mutex mutex = new Mutex();

    public void Execute(Context context)
    {
        lock (mutex)
        {
            // some work here
        }

        // some more work here
    }
}
Emond
  • 50,210
  • 11
  • 84
  • 115
Lee337
  • 45
  • 5

2 Answers2

2

If this really is the only use of said Mutex, then it does indeed not make sense. A normal object would have been enough.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
1

Why would someone use a new mutex just for locking?

Probably because someone in a tutorial said "Mutex", and they literally thought of the Mutex object. There is no benefit or any special effects to using it as a lock. Any reference type has an Object Header Word making it eligible for use as a lock object.

Side note - locking using a static object will cause any instance of your Job class to be blocked while another instance is holding that lock.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321