💾 Archived View for blitter.com › apl-books › APLX50 › APLX-manual › www.microapl.com › apl_help › c… captured on 2024-08-25 at 04:08:37.
⬅️ Previous capture (2022-07-17)
-=-=-=-=-=-=-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Signal events</TITLE> <META NAME="DESCRIPTION" CONTENT="APL language help page: Signal events"> <META NAME="KEYWORDS" CONTENT="Tasks,Multi-tasking,Signal events,apl,aplx,apl help"> <!-- %%COMMON_HEAD%% --> <META http-equiv="Content-Type" content="text/html; charset=utf-8"> <LINK rel="stylesheet" type="text/css" href="http://www.microapl.com/styles_apl_help.css"> <!-- %%END%%--> </HEAD> <body> <table> <tr> <td width="800" valign="top" colspan="2"> <center>Topic: <A HREF="ch.htm"><code>APLX Help</code></A> : <A HREF="ch_200.htm"><code>Multi-tasking support</code></A> : <A HREF="ch_200_060.htm"><code>Signal events</code></A> </center> <center> [ <A HREF="ch_200_050.htm">Previous</A> | <A HREF="ch_200_070.htm">Next</A> | <A HREF="ch.htm">Contents</A> | <A HREF="help_index.htm">Index</A> | <A HREF="http://www.microapl.co.uk/apl/index.html">APL Home</A> ]</center> <br></td> </tr> <tr> <td width="120"> <a href="http://www.microapl.co.uk/apl/index.html"><img height="68" border="0" width="119" src="MicroAPL_logo.gif" alt="www.microapl.co.uk"></a> </td> <td align="left" valign="bottom"> <h1>Signal events</h1> </td> </tr> <tr> <td width="800" valign="top" colspan="2"> <hr> <p>The main method of communication between child and parent tasks is through explicit 'signal' events. This mechanism allows the child task to send a message to the parent, and vice versa. This works as follows:</p> <ul> <li>If the parent wants to send a signal to the child, it invokes the <tt>Signal</tt> method in the child task object. This causes an <tt>onSignal</tt> event to trigger in the System object of the child task. <P></p> </li> <li>Conversely, if the child task wishes to send a signal to the parent, it invokes the <tt>Signal</tt> method in its own System object. This causes an <tt>onSignal</tt> event to trigger in the child task object of the parent.</li> </ul> <p>In both cases, the <tt>Signal</tt> method optionally takes an argument, which is any APL array (or an APLX overlay created using <code>⎕OV</code>). This is typically used to send a command to the other task, or to return a result to it. Any argument to the <tt>Signal</tt> method is available to the receiving task as the <code>⎕WARG</code> system variable during the execution of the callback.</p> <p>For example, suppose a child task is being used to carry out a time-consuming calculation. It starts by awaiting a signal to indicate that it should do the calculation, with the argument to the signal being the data to work on. When it has completed the calculation, it sends a signal back to the parent with the answer. The following sequence indicates how this might be done. </p> <p>First we need to do some setup. The parent task attaches a callback to the child task object:</p> <pre> ∇CALCDONE [1] 'The answer is ' ⎕WARG ∇ ChildTask.onSignal←'CALCDONE' </pre> <p>This will cause the <code>CALCDONE</code> function to be run when the child task signals that it has a result available, and the result will thus be printed.</p> <p>Similarly, the child task attaches a callback to its System object, waiting for a signal to indicate that it should carry out the calculation:</p> <pre> ∇DOCALC;RESULT;DATA [1] DATA←⎕WARG [2] RESULT←RUNMODEL DATA [3] '#' ⎕WI 'Signal' RESULT ∇ '#' ⎕WI 'onSignal' 'DOCALC' ⎕WE ¯1 </pre> <p>This will cause the <code>DOCALC</code> function to be run when the parent signals the child, using the data passed as the argument to the <tt>Signal</tt> method. When the calculation is complete, the child signals back to the parent. The child then sits in the <code>⎕WE</code> event loop waiting for signals to occur (this takes no CPU resource).</p> <p>To carry out the calculation, the parent signals the child, and then processes events (if it is not already running under <code>⎕WE</code>):</p> <pre> ChildTask.Signal THEDATA ⎕WE ¯1 </pre> <p>This triggers the child task to run the <code>DOCALC</code> function, and on completion the parent's <code>CALCDONE</code> function runs:</p> <pre>The answer is 42</pre> <p>In a real example, the parent task would be responding to other events (for example, user-interface events), and there might be a number of child tasks each simultaneously working on a different run of the model. If you have multiple child tasks running, you can use the task ID (<code>⎕EV[6]</code> in index origin 1) to distinguish between them in the callback function.</p> <p>There might also be a queue of signals waiting to be processed on either side. Note that there is a limit to the number of events which can be queued, typically around 200 events. If this maximum is exceeded, the oldest events are thrown away.</p> <hr> </td> </tr> <tr> <td width="800" valign="top" colspan="2"> <center>Topic: <A HREF="ch.htm"><code>APLX Help</code></A> : <A HREF="ch_200.htm"><code>Multi-tasking support</code></A> : <A HREF="ch_200_060.htm"><code>Signal events</code></A> </center> <center> [ <A HREF="ch_200_050.htm">Previous</A> | <A HREF="ch_200_070.htm">Next</A> | <A HREF="ch.htm">Contents</A> | <A HREF="help_index.htm">Index</A> | <A HREF="http://www.microapl.co.uk/apl/index.html">APL Home</A> ]</center> <br></td> </tr> </table> <!-- %%COMMON_BODY_TAIL%% --> <p class="copyright">Copyright © 1996-2010 MicroAPL Ltd</p> <!-- %%END%% --> </body> </html>