6.4 Concurrent Threads and Logic Variables

Concurrency is a way to organize computation based on the notion of concurrent processes. Concurrency is well-known from operating systems like UNIX which support multi-tasking in order to administrate multiple windows each of which runs in its own process. Oz supports concurrent computation on a high level of abstraction. The presentation of concurrency in this reader stays at the very surface of the phenomenon.

A process in Oz is called a thread. A thread is created when executing a sequences of Oz-statement sequentially. A thread may block until more information becomes available. At first sight blocking may seem to be a programming error. For instance, consider:

declare  F
X={F 2}  
{Browse 'this thread blocks'}  
{Browse variables(x:X f:F number:1)}

When feeding this piece of code at once, nothing is browsed. The problem is that the value of the variable F is unknown such that the application of {F 2} has to block. All followup statements of the same thread (code sequence) are also blocked until the free variable F gets assigned a value (i.e. gets bound).

Using the programming interface, you can easily feed another sequence of statements which then computes concurrently in its own thread.

F=fun{$ Y} Y*end

Now, the value of F has become known. Thereby, the first thread become active again and could executed its remaining two Browse-statements.

You can also create your own threads without using the Oz-Programming-Interface. This can be done by using the command:

thread ... end

For instance, the above example can be rewritten such that the blocking application does not block the subsequent statements.

declare X F
thread  
   X={F 2}  
   {Browse 'this thread blocks ...'}  
   {Browse variables(x:X f:F number:1)}
   {Browse '... but not forever'}  
end 
{Browse 'this thread does NOT block'}
F=fun{$ Y} Y*end

This example illustrates the creation of a new thread which first blocks until the free variable F gets bound by the main thread which runs concurrently to its newly sporned thread.

Threads in Oz threads communicate over shared logic variables which play the same role such as channels in CML or PICT. In Oz, you can also consider a thread as a hand-written propagator which adds information about the value of variables to a shared constraint store.


Denys Duchier, Claire Gardent and Joachim Niehren
Version 1.3.99 (20050412)