Product Site

12.4.4. Additional Examples

The following example uses REPLY in a method for a write-back cache.
Example 12.6. Method Write_Back
/* Method Write_Back                                 */
use arg data           /* Save data to be written    */
reply 0                /* Tell the sender all was OK */
self~disk_write(data)  /* Now write the data         */

The REPLY instruction returns control to the point at which method Write_Back was called, returning the result 0. The caller of method Write_Back continues processing from this point; meanwhile, method Write_Back also continues processing.

The following example uses a message object. It reads a line asynchronously into the variable nextline:
Example 12.7. Message to INFILE
mymsg = infile~start("READLINE") /* Gets message object to carry    */
/* message to INFILE                                                */
/* do other work */
nextline=mymsg~result            /* Gets result from message object */

This creates a message object that waits for the read to finish while the sender continues with other work. When the line is read, the mymsg message object obtains the result and holds it until the sender requests it.

Semaphores and monitors (bounded buffers) synchronize concurrency processes. Giving readers and writers concurrent access is a typical concurrency problem. The following sections show how to use guarded methods to code semaphore and monitor mechanisms and to provide concurrency for readers and writers.