18.10.2014 Views

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

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.

Tasks 291<br />

The execution of the above program can be visualized as Figure 20.1<br />

Thread_1 {Factorial(5)}<br />

Thread_2 {Factorial(7)}<br />

Ma<strong>in</strong> program thread<br />

Thread_3 {Prime(97)}<br />

Figure 20.1 Illustration of active threads <strong>in</strong> the above program.<br />

Once started, each of the threads will execute concurrently until the F<strong>in</strong>ish rendezvous is encountered,<br />

which is used to deliver the result to the <strong>in</strong>itiator of the tasks.<br />

Note:<br />

20.1.2 Task rendezvous<br />

The actual implementation of the concurrency will depend on the underly<strong>in</strong>g architecture, both<br />

software and hardware, of the platform on which the program is executed.<br />

The rendezvous mechanism is used for:<br />

• synchroniz<strong>in</strong>g two separate threads so that <strong>in</strong>formation may be exchanged.<br />

• synchroniz<strong>in</strong>g the execution of two threads.<br />

A rendezvous is achieved by one task hav<strong>in</strong>g an entry statement and the other task perform<strong>in</strong>g a call on this<br />

entry. For example, the code for a rendezvous to pass a Positive number to the task object thread_1 the<br />

code would be:<br />

Ma<strong>in</strong> program (client) which<br />

elaborates thread1<br />

Thread_1.Start(5);<br />

Body of task Thread_1 (server)<br />

accept Start(F:<strong>in</strong> Positive) do<br />

Factorial := F;<br />

end Start;<br />

To achieve this effect, one of the threads of control will be suspended until the other thread catches up. Then at<br />

the rendezvous, data, <strong>in</strong> this case the number 5, is transferred between the tasks. The code between do and end is<br />

executed with the client task suspended. After the code between do and end has been executed both tasks resume<br />

their <strong>in</strong>dependent execution.<br />

This rendezvous between the two tasks is illustrated <strong>in</strong> Figure 20.2 <strong>in</strong> which the ma<strong>in</strong> program task<br />

rendezvous with an <strong>in</strong>stance of the task Factorial.<br />

© M A Smith - May not be reproduced without permission

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

Saved successfully!

Ooh no, something went wrong!