<div dir="ltr">This is an idea using automatic cleanup of interfaces:<br><br>procedure foo; <- lock<br>
begin<br>
synchronize(cs);<br>
[...]<br>end; <- unlock<br>
<br>or<br><br>procedure foo; <- lock<br>
begin<br>
with synchronize(cs) do <- lock<br>
begin<br>
[...]<br>
end; <- unlock<br>
end;<br>
<br><br>{ TLock }<br><br>type<br> TLock = class(TInterfacedObject)<br> private<br> FCriticalSection: TCriticalSection;<br> public<br> constructor Create(cs: TCriticalSection); virtual;<br> destructor Destroy; override;<br>
end;<br><br>constructor TLock.Create(cs: TCriticalSection);<br>begin<br> FCriticalSection := cs;<br> cs.Acquire;<br>end;<br><br>destructor TLock.Destroy;<br>begin<br> FCriticalSection.Release;<br> inherited;<br>end;<br>
<br>function synchronize(cs: TCriticalSection): IUnknown;<br>begin<br> Result := TLock.Create(cs);<br>end;<br><br><br><br><br><br><br><div class="gmail_quote">2008/7/31 tom_at_work <span dir="ltr"><<a href="mailto:tom_at_work@gmx.at">tom_at_work@gmx.at</a>></span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Hello,<br>
<br>
Am 31.07.2008 um 11:56 schrieb Florian Klaempfl:<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Vinzent Höfler schrieb:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
-------- Original-Nachricht -------<div class="Ih2E3d"><br>
OTOH, it's looks about the same as in Java and even Java now has<br>
explicit Locks, because "synchronized" simply isn't efficient nor<br>
flexible enough... ;)<br>
</div></blockquote>
<br>
Since I don't use java, what's the difference to locks?<br>
<br>
</blockquote>
<br>
In practice imo not too much, synchronized is just a convenience functionality, exactly like the one you proposed.<br>
<br>
In short the difference:<br>
<br>
Java always locks on monitors i.e. guards which are available for any object; Pre 1.5 the only way to lock was using some syntax additions. The syntax allows per method (class or instance) or per block scope, i.e.<br>
<br>
synchronized void doSomething() { ... } // method level: monitor object is either "this" or this.class object<br>
<br>
void doSomething() {<br>
...<br>
synchronized (monitor) { // on block level; monitor object is "this" if not specified otherwise the given object<br>
}<br>
...<br>
}<br>
<br>
With 1.5 they added a "Lock" interface which provides the usual lock()/trylock()/unlock() methods which should be self-explaining.<br>
This for example allows interlocked locks if required<br>
<br>
lock1.lock();<br>
...<br>
lock2.lock();<br>
...<br>
lock1.unlock();<br>
...<br>
lock2.unlock();<br>
<br>
<br>
Hth,<br>
Thomas_______________________________________________<div><div></div><div class="Wj3C7c"><br>
fpc-devel maillist - <a href="mailto:fpc-devel@lists.freepascal.org" target="_blank">fpc-devel@lists.freepascal.org</a><br>
<a href="http://lists.freepascal.org/mailman/listinfo/fpc-devel" target="_blank">http://lists.freepascal.org/mailman/listinfo/fpc-devel</a><br>
</div></div></blockquote></div><br></div>