Critical sections (was: RE: Lots of concurrency)

Lex Spoon lex at cc.gatech.edu
Fri Oct 26 16:20:19 UTC 2001


"Stephen Pair" <spair at advantive.com> wrote:
> > > Speaking of critical sections, some dialects of Smalltalk 
> > allow you to
> > > write:
> > > 
> > > 	[ balance := balance - 500 ] critical.
> > > 
> > 
> > Well, the semantics of this are pretty awkward in Smalltalk.  
> > Also, read some of the recent posts about Java's synchronized 
> > keyword -- it might not be as useful in practice as it looks 
> > on a small scale.
> 
> Seems pretty clear to me, I would assume that this block, and only this
> block, is protected. 

I guess the meaning is clear, but it goes against normal Smalltalk
interpretation.  If you write a normal #critical method, it will get
sent to a different dynamic block every time the above line of code is
executed.  That's what a Smalltalker will expect.  However, for this
method to do what is probably intended, it needs to search back and
figure out which *static* block it is associated with.  This can be made
to work if you have some Compiler support, but....  maybe a syntax can
be devised that fits the normal Smalltalk execution of blocks.



[...snip the existing #critical: method ...]

> > Anyway, much better than even this is to rephrase your system 
> > in terms of message passing, as discussed elsewhere in this 
> > thread.  Critical sections are just really tough to work with by hand.
> 
> I'd be interested to see how you would do that with this bank account
> withdrawal example.  I can think of one potential solution, but I think
> it would be more awkward...a case of the cure being worse than the
> disease so to speak.
> 

Critical sections are extremely general.   (heh, a lot like assembly
language!)  :)  A cheezy solution is:

	globalSemaphore critical: [ "blahblah"  ]


Or if you want fine-grained stuff, you can canonicalize the order of the
banks and do:

	bank1 semaphore critical: [  bank2 semaphore critical: [ ... ] ]


And of course, you can implement most any other synchronization system
on top of these mutexes.  In fact, implementing a more convenient
synhcronization system is probably the best way to use a system based on
mutexes.  :)

-Lex




More information about the Squeak-dev mailing list