Goober
Goober

Reputation: 13506

Difference Between Monitor & Lock?

What's the difference between a monitor and a lock?

If a lock is simply an implementation of mutual exclusion, then is a monitor simply a way of making use of the waiting time inbetween method executions?

A good explanation would be really helpful thanks....

regards

Upvotes: 11

Views: 13330

Answers (9)

Martin
Martin

Reputation: 1296

As far as I have researched so far, monitor is a set of principles for thread synchronization, while locks are, along with "thread cooperation" facilities like wait and notify, the way monitors are implemented in Java. So effectively, if we try to form the exact relationship between the two notions, locks are one part of the implementation of monitors (the other being wait and notify mechanisms). Please correct me if I'm wrong, but I would really appreciate if the correction is very specific.

Upvotes: 0

John Gietzen
John Gietzen

Reputation: 49564

For example in C# .NET a lock statement is equivalent to:

Monitor.Enter(object);
try
{
    // Your code here...
}
finally
{
    Monitor.Exit(object);
}

However, keep in mind that Monitor can also Wait() and Pulse(), which are often useful in complex multithreading situations.

Edit: In later versions of the .NET framework, this was changed to:

bool lockTaken = false;
try
{
    Monitor.Enter(object, ref lockTaken);
    // Your code here...
}
finally
{
    if (lockTaken)
    {
        Monitor.Exit(object);
    }
}

Upvotes: 14

user3008389
user3008389

Reputation: 11

Monitor is the concept and Lock is the actual implementation.

Upvotes: 0

Ali.Rashidi
Ali.Rashidi

Reputation: 1462

There is no difference, lock generates Monitor.Enter and Monitor.Exit within a try/finally block. Using Monitor over lock allows you to fine tune because it has Pulse and PulseAll. You can also have alternate processing should you be unable to acquire the lock with TryEnter.

Upvotes: 0

ganadara
ganadara

Reputation: 39

Lock focus on only mutual exculsion, but Moniter provides mutual exclusion automatically.

So we don't need to worry of using mutual exclusion in Monitor. Instead of ME, we need to consern of sycronzing only when we do programming.

Moniter provides more systematical way of programming. It, therefor, is more advanced one.

Upvotes: -1

Dan Mantyla
Dan Mantyla

Reputation: 1870

Monitors is a programming-language construct that does the same thing as semiphores/locks, but Monitors control the shared data by synchronizing at run time. In contrast, locks protect the shared data by just "spinning" which can lead to poor CPU utilization.

Upvotes: 0

Bastien Léonard
Bastien Léonard

Reputation: 61783

A lock ensures mutual exclusion.

A monitor associates the data to be protected and the mutual exclusion and synchronization primitives required to protect accesses to the data.
Synchronization is used e.g. when you need one thread to wait until an event occurs (e.g., wait until another thread places an item in a queue).

Upvotes: 0

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84199

Monitors are compiler-assisted "semi-automatic" locks. They allow one to declare synchronized methods on classes, etc. This is just a different approach to providing mutual exclusion. I found this book to be the most thorough explanation of the concepts, even though it's mostly geared towards OS developers.

Upvotes: 1

Jeff Moser
Jeff Moser

Reputation: 20053

They're related. For example, in C# the lock statement is a simple try-finally wrapper around entering a Monitor and exiting one when done.

Upvotes: 4

Related Questions