Re: Status of thread module?

Daniel W. Connolly (connolly@hal.com)
Fri, 18 Mar 1994 13:00:05 -0600

In message <9403181338.ZM5840@styx.hks.com>, Uttam M. Narsu writes:
>>
>> I have experience with an implementation of <pthread.h> style threads
>> in C. But what I'd like to see is the Modula 3 Thread interface.
>
>Please explain the differences. What does the M3 interface offer? (I'm not
>familiar with it)

There's no significant functionality in the M3 interface that's not
in <pthread.h>. It's just a much nicer integration with the language,
and a python thread interface could do the same sort of thing.

The Modula-3 interface looks something like (don't have a book handy):

INTERFACE Thread;
TYPE T <: REFANY; (* represents a thread *)
TYPE Mutex = MUTEX; (* built into the language; <: ROOT *)
TYPE Condition <: ROOT;
TYPE Closure = OBJECT METHODS
apply() : T; (* spawn a thread *)
END;

EXCEPTION Alerted;

PROCEDURE Acquire(m : Mutex);
PROCEDURE Release(m : Mutex);
PROCEDURE Wait(c : Condition; m: Mutex) RAISES Alerted;
PROCEDURE Join(thread: T);
PROCEDURE Alert(thread: T);

END Thread.

Plus there's a LOCK statement:

LOCK m DO
...
END

that's equivalent to:

Thread.Acquire(m); TRY
...
FINALLY
Thread.Release(m);
END

The idea is that if you've got some data you want to protect with
a mutex, you can do:

IMPORT Thread
TYPE Precious = Thread.Mutex OBJECT
data : INTEGER;
END;

VAR p : Precious;

LOCK p DO
INC(p.data)
END

in python, I expect it would look something like:

import Thread
from Thread import LOCK, END

class Precious(Thread.Mutex):
def __init__(self, n):
self.data = n

p = Precious(10)
LOCK(p); try: # or better yet, add lock statement to grammar
p.n = p.n + 1 # is there a ++ equiv in python?
finally: END(p)

Daniel W. Connolly "We believe in the interconnectedness of all things"
Software Enginner, Hal Computer Systems (512) 834-9962 x5010
<connolly@hal.com> http://www.hal.com/%7Econnolly/index.html