19.11.2014 Views

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

stop exception and transforms it into a deferred Stopped exception. This is visible to the programmer and should be<br />

caught by invoking the val method on the thread object. Implicit threads are terminated only if another thread in the<br />

group completes abruptly, and the threads that are terminated are ignored for the purposes of the completion of the<br />

group.<br />

Typical code for stopping a thread looks something like the following example:<br />

x : Z64 := 0<br />

t = spawn do<br />

try<br />

atomic if x = 0 then abort() else () end<br />

finally<br />

x := 1<br />

end<br />

end<br />

t.stop()<br />

try<br />

t.val()<br />

catch s<br />

Stopped ⇒ x += 2; x<br />

end<br />

Here the spawned thread t blocks until it is killed by the call to t.stop() ; it sets x to 1 in the finally clause before<br />

exiting. In this case, the call to t.val() will throw Stopped, which is caught, causing 2 to be added to x and returning<br />

3.<br />

Note that there is a race in the above code, so the try block in t may not have been entered when t.stop() is called,<br />

causing x to be 2 at the end of execution. Note also that the call to t.stop() occurs asynchronously; in the absence of<br />

the call to t.val() , the spawning thread would not have waited for t to complete.<br />

32.7 Placing Threads<br />

A thread can be placed in a particular region by using an at expression:<br />

(v, w) = (a i ,<br />

at a.region(j) do<br />

a j<br />

end)<br />

In this example, two implicit threads are created; the first computes a i locally, the second computes a j in the region<br />

where the j th element of a resides, specified by a.region(j) . <strong>The</strong> expression after at must return a value of type<br />

Region, and the block immediately following do is run in that region; the result of the block is the result of the at<br />

expression as a whole. Often it is more graceful to use the also do construct (described in Section 13.12) in these<br />

cases:<br />

do<br />

v := a i<br />

alsoat a.region(j) do<br />

w := a j<br />

end<br />

We can also use at with a spawn expression:<br />

220

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!