difference latch and lock

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

difference latch and lock

alarmnummer
Who can tell me what the difference between a Latch and a Lock is. I
have read multiple times that a Latch is considered a more lightweight
lock than the lock (maybe only in the context of databases and heavy
weight locks).

But in Java all Locks and synchronized blocks are lightweight.. so are
there differences?
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: difference latch and lock

David Holmes-3
Peter,

Depends exactly what you mean by "latch". A latch or gate is a
synchronization barrier that is either open or closed. If open anyone can
pass, if closed no one can pass. A Lock is intended to let through one
thread at a time.

I don't know what they meant by a latch being more lightweight than a lock
???

David Holmes

> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]]On Behalf Of Peter
> Veentjer
> Sent: Thursday, 31 August 2006 6:14 AM
> To: [hidden email]
> Subject: [concurrency-interest] difference latch and lock
>
>
> Who can tell me what the difference between a Latch and a Lock is. I
> have read multiple times that a Latch is considered a more lightweight
> lock than the lock (maybe only in the context of databases and heavy
> weight locks).
>
> But in Java all Locks and synchronized blocks are lightweight.. so are
> there differences?
> _______________________________________________
> Concurrency-interest mailing list
> [hidden email]
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: difference latch and lock

alarmnummer
I don't know what a latch is supposed to do. I see the term in
documentation (especially in concurrency control documentation of
Oracle). And I think a Latch can be compared to a Lock, but I don't
know for sure and I can't find any usefull documentation about what a
Latch is supposed to do.

So I want to know what it does.

On 8/30/06, David Holmes <[hidden email]> wrote:

> Peter,
>
> Depends exactly what you mean by "latch". A latch or gate is a
> synchronization barrier that is either open or closed. If open anyone can
> pass, if closed no one can pass. A Lock is intended to let through one
> thread at a time.
>
> I don't know what they meant by a latch being more lightweight than a lock
> ???
>
> David Holmes
>
> > -----Original Message-----
> > From: [hidden email]
> > [mailto:[hidden email]]On Behalf Of Peter
> > Veentjer
> > Sent: Thursday, 31 August 2006 6:14 AM
> > To: [hidden email]
> > Subject: [concurrency-interest] difference latch and lock
> >
> >
> > Who can tell me what the difference between a Latch and a Lock is. I
> > have read multiple times that a Latch is considered a more lightweight
> > lock than the lock (maybe only in the context of databases and heavy
> > weight locks).
> >
> > But in Java all Locks and synchronized blocks are lightweight.. so are
> > there differences?
> > _______________________________________________
> > Concurrency-interest mailing list
> > [hidden email]
> > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: difference latch and lock

alarmnummer
Ps: a 'lock' in Oracle is something that is kept for a longer period
of time (in most cases a lock is maintained untill the transaction
completes).

Maybe this is the reason why they have introduced Locks and Latches?

A Latch is a Lock that is invisible from the outside and only locked
for a small period of time (to prevent multiple threads screwing
around with internal system structures)

A (Database) Lock is a Lock that is visible from the outside. If I
have locked a record, you can't update it untill I have released the
lock.A Database lock prevent isolation problems: acid)

Could that be the reason why there is a difference between Latches
(internal Locks) and Locks (database locks)?

On 8/30/06, Peter Veentjer <[hidden email]> wrote:

> I don't know what a latch is supposed to do. I see the term in
> documentation (especially in concurrency control documentation of
> Oracle). And I think a Latch can be compared to a Lock, but I don't
> know for sure and I can't find any usefull documentation about what a
> Latch is supposed to do.
>
> So I want to know what it does.
>
> On 8/30/06, David Holmes <[hidden email]> wrote:
> > Peter,
> >
> > Depends exactly what you mean by "latch". A latch or gate is a
> > synchronization barrier that is either open or closed. If open anyone can
> > pass, if closed no one can pass. A Lock is intended to let through one
> > thread at a time.
> >
> > I don't know what they meant by a latch being more lightweight than a lock
> > ???
> >
> > David Holmes
> >
> > > -----Original Message-----
> > > From: [hidden email]
> > > [mailto:[hidden email]]On Behalf Of Peter
> > > Veentjer
> > > Sent: Thursday, 31 August 2006 6:14 AM
> > > To: [hidden email]
> > > Subject: [concurrency-interest] difference latch and lock
> > >
> > >
> > > Who can tell me what the difference between a Latch and a Lock is. I
> > > have read multiple times that a Latch is considered a more lightweight
> > > lock than the lock (maybe only in the context of databases and heavy
> > > weight locks).
> > >
> > > But in Java all Locks and synchronized blocks are lightweight.. so are
> > > there differences?
> > > _______________________________________________
> > > Concurrency-interest mailing list
> > > [hidden email]
> > > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
> >
> >
>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: difference latch and lock

David Holmes-3
Peter,

It may be that they are using "latch" as a lightweight internal lock. Pretty
poor choice of name if you ask me though.

Cheers,
David Holmes

> -----Original Message-----
> From: Peter Veentjer [mailto:[hidden email]]
> Sent: Thursday, 31 August 2006 7:32 AM
> To: [hidden email]
> Cc: [hidden email]
> Subject: Re: [concurrency-interest] difference latch and lock
>
>
> Ps: a 'lock' in Oracle is something that is kept for a longer period
> of time (in most cases a lock is maintained untill the transaction
> completes).
>
> Maybe this is the reason why they have introduced Locks and Latches?
>
> A Latch is a Lock that is invisible from the outside and only locked
> for a small period of time (to prevent multiple threads screwing
> around with internal system structures)
>
> A (Database) Lock is a Lock that is visible from the outside. If I
> have locked a record, you can't update it untill I have released the
> lock.A Database lock prevent isolation problems: acid)
>
> Could that be the reason why there is a difference between Latches
> (internal Locks) and Locks (database locks)?
>
> On 8/30/06, Peter Veentjer <[hidden email]> wrote:
> > I don't know what a latch is supposed to do. I see the term in
> > documentation (especially in concurrency control documentation of
> > Oracle). And I think a Latch can be compared to a Lock, but I don't
> > know for sure and I can't find any usefull documentation about what a
> > Latch is supposed to do.
> >
> > So I want to know what it does.
> >
> > On 8/30/06, David Holmes <[hidden email]> wrote:
> > > Peter,
> > >
> > > Depends exactly what you mean by "latch". A latch or gate is a
> > > synchronization barrier that is either open or closed. If
> open anyone can
> > > pass, if closed no one can pass. A Lock is intended to let through one
> > > thread at a time.
> > >
> > > I don't know what they meant by a latch being more
> lightweight than a lock
> > > ???
> > >
> > > David Holmes
> > >
> > > > -----Original Message-----
> > > > From: [hidden email]
> > > > [mailto:[hidden email]]On
> Behalf Of Peter
> > > > Veentjer
> > > > Sent: Thursday, 31 August 2006 6:14 AM
> > > > To: [hidden email]
> > > > Subject: [concurrency-interest] difference latch and lock
> > > >
> > > >
> > > > Who can tell me what the difference between a Latch and a Lock is. I
> > > > have read multiple times that a Latch is considered a more
> lightweight
> > > > lock than the lock (maybe only in the context of databases and heavy
> > > > weight locks).
> > > >
> > > > But in Java all Locks and synchronized blocks are
> lightweight.. so are
> > > > there differences?
> > > > _______________________________________________
> > > > Concurrency-interest mailing list
> > > > [hidden email]
> > > > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
> > >
> > >
> >

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: difference latch and lock

Jean Morissette-2
In reply to this post by David Holmes-3
In the database context, locks are related to transactions, while
latchs are short lived locks used, by example, to access the indices.

Jean Morissette

2006/8/30, David Holmes <[hidden email]>:

> Peter,
>
> Depends exactly what you mean by "latch". A latch or gate is a
> synchronization barrier that is either open or closed. If open anyone can
> pass, if closed no one can pass. A Lock is intended to let through one
> thread at a time.
>
> I don't know what they meant by a latch being more lightweight than a lock
> ???
>
> David Holmes
>
> > -----Original Message-----
> > From: [hidden email]
> > [mailto:[hidden email]]On Behalf Of Peter
> > Veentjer
> > Sent: Thursday, 31 August 2006 6:14 AM
> > To: [hidden email]
> > Subject: [concurrency-interest] difference latch and lock
> >
> >
> > Who can tell me what the difference between a Latch and a Lock is. I
> > have read multiple times that a Latch is considered a more lightweight
> > lock than the lock (maybe only in the context of databases and heavy
> > weight locks).
> >
> > But in Java all Locks and synchronized blocks are lightweight.. so are
> > there differences?
> > _______________________________________________
> > Concurrency-interest mailing list
> > [hidden email]
> > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
> _______________________________________________
> Concurrency-interest mailing list
> [hidden email]
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: difference latch and lock

alarmnummer
In reply to this post by alarmnummer
Thanks for your answer Brian. Could you give an example how a Latch
would be used?

On 8/31/06, Brian Goetz <[hidden email]> wrote:

> Latches and locks are different.  It is easiest to think of them in
> terms of their state machines.  A lock goes from unlocked to locked and
> back to unlocked with the right sequence of lock and unlock operations.
>  A latch goes from closed to open after some sequence of state
> transitions, and NEVER GOES BACK.  Once it transitions to the terminal
> state, it never changes.
>
> Both offer blocking behavior.  For Lock, if you try to acquire an
> already-held lock, you block until the lock is available.  For Latch, if
> you try to wait for the latch, you block until someone else puts the
> latch in the terminal (open) state.
>
>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: difference latch and lock

David Holmes-3
I think we're encountering a number of different uses of the word "latch".

As Brian describes one notion of state "latching" is that it progresses to a
terminal value from whence it no longer changes. It is permanently
"latched". A synchronization object that behaves in this way is the
CountDownLatch - once "open" it never closes and can't be reset.

More generally though latches can be reset - consider a digital flip-flop
such as a "gated D-latch", the flip-flop latches the value of the data when
the gate is pulsed/strobed; if you change the data without changing the gate
then the latched value is unchanged, but change the gate and the latched
value is updated.

In synchronization object terms a latch is sometimes called a "gate" - the
connotation being that if the gate is open anyone/everyone can pass through;
while if it is shut no one can pass through. The CountDownLatch operates
this way, but a more general "gate" is the CyclicBarrier which can also be
reset (and automatically does so). Of course the semantics of CountDownLatch
and CyclicBarrier are somewhat different. CyclicBarrier is what can be
called a "weighted gate" or "weighted bucket" - it is set up to expect N
threads to arrive, when they arrive they have sufficient "weight" to open
the gate, or tip the bucket - in this case the gate/bucket is spring-loaded
and closes/rights-itself as soon as the threads leave, so it is ready for
the next set of threads to use. CountDownLatch on the other hand is like a
gate with multiple padlocks - when the last padlock is removed, the gate
opens and it stays open. Aren't these analogies quaint :-) We could have
defined CountDownLatch to allow reset but reset semantics are messy and
usually not needed for CDL usage, in contrast barrier designs typically
always use the barrier multiple times.

It seems the database folk are using the term "latch" for lightweight lock,
which is an uncommon usage from a synchronization perspective and a poor
choice in my view, though arguably there is an analogy between "locking a
door/window" and just "latching it shut".  In that sense "latching" is a
weaker form of "locking". But I don't like the usage.

Cheers,
David Holmes

> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]]On Behalf Of Peter
> Veentjer
> Sent: Friday, 1 September 2006 3:44 PM
> To: Brian Goetz; [hidden email]
> Subject: Re: [concurrency-interest] difference latch and lock
>
>
> Thanks for your answer Brian. Could you give an example how a Latch
> would be used?
>
> On 8/31/06, Brian Goetz <[hidden email]> wrote:
> > Latches and locks are different.  It is easiest to think of them in
> > terms of their state machines.  A lock goes from unlocked to locked and
> > back to unlocked with the right sequence of lock and unlock operations.
> >  A latch goes from closed to open after some sequence of state
> > transitions, and NEVER GOES BACK.  Once it transitions to the terminal
> > state, it never changes.
> >
> > Both offer blocking behavior.  For Lock, if you try to acquire an
> > already-held lock, you block until the lock is available.  For Latch, if
> > you try to wait for the latch, you block until someone else puts the
> > latch in the terminal (open) state.
> >
> >
> _______________________________________________
> Concurrency-interest mailing list
> [hidden email]
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: difference latch and lock

Brian Goetz
In reply to this post by alarmnummer
See JCiP 5.5.1 and 5.5.2.  Also 8.5.1 and 14.2.5 for more examples.

Peter Veentjer wrote:

> Thanks for your answer Brian. Could you give an example how a Latch
> would be used?
>
> On 8/31/06, Brian Goetz <[hidden email]> wrote:
>> Latches and locks are different.  It is easiest to think of them in
>> terms of their state machines.  A lock goes from unlocked to locked and
>> back to unlocked with the right sequence of lock and unlock operations.
>>  A latch goes from closed to open after some sequence of state
>> transitions, and NEVER GOES BACK.  Once it transitions to the terminal
>> state, it never changes.
>>
>> Both offer blocking behavior.  For Lock, if you try to acquire an
>> already-held lock, you block until the lock is available.  For Latch, if
>> you try to wait for the latch, you block until someone else puts the
>> latch in the terminal (open) state.
>>
>>
> _______________________________________________
> Concurrency-interest mailing list
> [hidden email]
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest