21 Concurrent Programming

The goal of this chapter is twofold. One the one hand side, we would like to introduce some general ideas of concurrent programming and how to implement them in Oz. On the other hand side, we are giving the basics needed for the concurrent chart parser which will be presented in a later Chapter. For this purpose, we present an abstract model of a concurrent agenda.

21.1 What is Concurrency

Concurrency is an way to organise 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.

Concurrent processes can be executed in parallel whenever they are independent. Whether concurrent processes are executed in parallel matters for efficiency only. Concurrency is an interesting way in which to organise program and independent of parallel or interleaving execution. For instance, when you use a Sparc 10 with 2 processors then UNIX can run two processes in parallel even though you might not be aware of it.

Oz supports concurrent computation on a high level of abstraction. A process in Oz is called a thread. As in most other programming languages (except Oz1 or AKL), process creation is explicit in Oz2. In Oz processes communicate over logic variables residing in a shared constraint store. Logic variables play the same role as channels (for instance in CML or PICT).



21.5 Exception Handling

Suppose, you want to kill a running process. But how can you do this? The answer is that you have to use exception handling. This is a control mechanism also know form SML and Java. A typical application of exception handling is to catch programming errors at run time and to execute some code which keeps the system in a consistent state. But there is much more you can do with exception handling, for instance killing arbitrary processes.

The basic idea of exception handling is as follows: You can through an exception in a thread by using the construct raise  end and catch this exception with the construct try  catch end. For instance try the following:

declare  
proc{Process} {Browse started} end 
proc{Loop}{Loop}end 
 
try   
{Process} raise done end {Loop}
catch X then {Browse '** '#X#' **'}   
finally {Browse 'does not loop'}
end

For more about exception handling can be found in Chapter 5 of Seif Haridis Tutorial.



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