[m-dev.] Stream2000

Peter Ross peter.ross at miscrit.be
Thu Nov 2 20:53:09 AEDT 2000


On Thu, Nov 02, 2000 at 08:45:47PM +1100, Fergus Henderson wrote:
> On 01-Nov-2000, Peter Ross <peter.ross at miscrit.be> wrote:
> > On Wed, Nov 01, 2000 at 08:20:15AM +1100, Fergus Henderson wrote:
> > > On 31-Oct-2000, Peter Ross <peter.ross at miscrit.be> wrote:
> > > > :- pragma promise_pure(putback_putback_char/4).
> > > > putback_putback_char(pb(_Stream, MPutbackChars), Char) -->
> > > > 	{ impure get_mutvar(MPutbackChars, PutbackChars) },
> > > > 	{ impure set_mutvar(MPutbackChars, [Char | PutbackChars] ) }.
> > > 
> > > Both of these operations are non-atomic and so might result in
> > > inconsistent behaviour if performed simultaneously from multiple
> > > threads.
> >
> > Using the concurrency mutvar would avoid this problem.
> 
> Using mutvar type from extras/concurrency would guarantee that
> each get_mutvar or set_mutvar operation is atomic, but it
> wouldn't guarantee that the putback_char operation itself is atomic,
> and so you might get inconsistent results where e.g.
> two threads both try to put back chars on the stream
> at the same time, and both operations succeed,
> but only one char has actually been pushed,
> because the order of operations went
> 
> time	thread 1		thread 2
> 1	get_mutvar(A, B)
> 2				get_mutvar(A, B)
> 3	set_mutvar(A, [C1|B])
> 4				set_mutvar(A, [C2|B])
> 
> and the second set_mutvar thus overwrote the value
> set in the first set_mutvar.
> 
The concurrency mutvars have the additional property that the mutvar is
either full or empty.  Attempting to take from an empty mutvar blocks
until the mutvar is full again, hence the second call to get_mutvar will
block until the first call to set_mutvar fills the mutvar again. So the
operation would be atomic.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list