| 1 | <?xml version="1.0" encoding="UTF-8"?> |
| 2 | |
| 3 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
| 4 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
| 5 | |
| 6 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> |
| 7 | <head> |
| 8 | <title>GAP (hpc) - Chapter 6: Thread functions</title> |
| 9 | <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> |
| 10 | <meta name="generator" content="GAPDoc2HTML" /> |
| 11 | <link rel="stylesheet" type="text/css" href="manual.css" /> |
| 12 | <script src="manual.js" type="text/javascript"></script> |
| 13 | <script type="text/javascript">overwriteStyle();</script> |
| 14 | </head> |
| 15 | <body class="chap6" onload="jscontent()"> |
| 16 | |
| 17 | |
| 18 | <div class="chlinktop"><span class="chlink1">Goto Chapter: </span><a href="chap0.html">Top</a> <a href="chap1.html">1</a> <a href="chap2.html">2</a> <a href="chap3.html">3</a> <a href="chap4.html">4</a> <a href="chap5.html">5</a> <a href="chap6.html">6</a> <a href="chap7.html">7</a> <a href="chap8.html">8</a> <a href="chap9.html">9</a> <a href="chap10.html">10</a> <a href="chap11.html">11</a> <a href="chapInd.html">Ind</a> </div> |
| 19 | |
| 20 | <div class="chlinkprevnexttop"> <a href="chap0.html">[Top of Book]</a> <a href="chap0.html#contents">[Contents]</a> <a href="chap5.html">[Previous Chapter]</a> <a href="chap7.html">[Next Chapter]</a> </div> |
| 21 | |
| 22 | <p id="mathjaxlink" class="pcenter"><a href="chap6_mj.html">[MathJax on]</a></p> |
| 23 | <p><a id="X841085357B854E1C" name="X841085357B854E1C"></a></p> |
| 24 | <div class="ChapSects"><a href="chap6.html#X841085357B854E1C">6 <span class="Heading">Thread functions</span></a> |
| 25 | <div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap6.html#X841085357B854E1C">6.1 <span class="Heading">Thread functions</span></a> |
| 26 | </span> |
| 27 | <div class="ContSSBlock"> |
| 28 | <span class="ContSS"><br /><span class="nocss"> </span><a href="chap6.html#X7A541B6C84980B7D">6.1-1 CreateThread</a></span> |
| 29 | <span class="ContSS"><br /><span class="nocss"> </span><a href="chap6.html#X81DFC84779CD1C1E">6.1-2 WaitThread</a></span> |
| 30 | <span class="ContSS"><br /><span class="nocss"> </span><a href="chap6.html#X7982BA8D782C171B">6.1-3 CurrentThread</a></span> |
| 31 | <span class="ContSS"><br /><span class="nocss"> </span><a href="chap6.html#X80FA498D866028CB">6.1-4 ThreadID</a></span> |
| 32 | <span class="ContSS"><br /><span class="nocss"> </span><a href="chap6.html#X787679BC7D36FAA3">6.1-5 KillThread</a></span> |
| 33 | <span class="ContSS"><br /><span class="nocss"> </span><a href="chap6.html#X7BC284107E56A01B">6.1-6 PauseThread</a></span> |
| 34 | <span class="ContSS"><br /><span class="nocss"> </span><a href="chap6.html#X80D590EC78C93F94">6.1-7 ResumeThread</a></span> |
| 35 | <span class="ContSS"><br /><span class="nocss"> </span><a href="chap6.html#X8521A5777CCD0B37">6.1-8 InterruptThread</a></span> |
| 36 | <span class="ContSS"><br /><span class="nocss"> </span><a href="chap6.html#X80C207B387CA888E">6.1-9 SetInterruptHandler</a></span> |
| 37 | <span class="ContSS"><br /><span class="nocss"> </span><a href="chap6.html#X78E5F5987BEFACE6">6.1-10 NewInterruptID</a></span> |
| 38 | <span class="ContSS"><br /><span class="nocss"> </span><a href="chap6.html#X863799F07D55BAE0">6.1-11 MAX_INTERRUPT</a></span> |
| 39 | </div></div> |
| 40 | </div> |
| 41 | |
| 42 | <h3>6 <span class="Heading">Thread functions</span></h3> |
| 43 | |
| 44 | <p>HPC-GAP has low-level functionality to support explicit creation of threads. In practice, programmers should use higher-level functionality, such as tasks, to describe concurrency. The thread functions described here exist to facilitate the construction of higher level libraries and are not meant to be used directly.</p> |
| 45 | |
| 46 | <p><a id="X841085357B854E1C" name="X841085357B854E1C"></a></p> |
| 47 | |
| 48 | <h4>6.1 <span class="Heading">Thread functions</span></h4> |
| 49 | |
| 50 | <p><a id="X7A541B6C84980B7D" name="X7A541B6C84980B7D"></a></p> |
| 51 | |
| 52 | <h5>6.1-1 CreateThread</h5> |
| 53 | |
| 54 | <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ CreateThread</code>( <var class="Arg">func</var>[, <var class="Arg">arg1</var>, <var class="Arg">...</var>, <var class="Arg">argn</var>] )</td><td class="tdright">( function )</td></tr></table></div> |
| 55 | <p>New threads are created with the function <code class="func">CreateThread</code>. The thread takes at least one function as its argument that it will call in the newly created thread; it also accepts zero or more parameters that will be passed to that function.</p> |
| 56 | |
| 57 | <p>The function returns a thread object describing the thread.</p> |
| 58 | |
| 59 | <p>Only a finite number of threads can be active at a time (that limit is system-dependent). To reclaim the resources occupied by one thread, use the <code class="func">WaitThread</code> (<a href="chap6.html#X81DFC84779CD1C1E"><span class="RefLink">6.1-2</span></a>) function.</p> |
| 60 | |
| 61 | <p><a id="X81DFC84779CD1C1E" name="X81DFC84779CD1C1E"></a></p> |
| 62 | |
| 63 | <h5>6.1-2 WaitThread</h5> |
| 64 | |
| 65 | <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ WaitThread</code>( <var class="Arg">threadID</var> )</td><td class="tdright">( function )</td></tr></table></div> |
| 66 | <p>The <code class="func">WaitThread</code> function waits for the thread identified by <var class="Arg">threadID</var> to finish; it does not return any value. When it returns, it returns all resources occupied by the thread it waited for, such as thread-local memory and operating system structures, to the system.</p> |
| 67 | |
| 68 | <p><a id="X7982BA8D782C171B" name="X7982BA8D782C171B"></a></p> |
| 69 | |
| 70 | <h5>6.1-3 CurrentThread</h5> |
| 71 | |
| 72 | <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ CurrentThread</code>( )</td><td class="tdright">( function )</td></tr></table></div> |
| 73 | <p>The <code class="func">CurrentThread</code> function returns the thread object for the current thread.</p> |
| 74 | |
| 75 | <p><a id="X80FA498D866028CB" name="X80FA498D866028CB"></a></p> |
| 76 | |
| 77 | <h5>6.1-4 ThreadID</h5> |
| 78 | |
| 79 | <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ ThreadID</code>( <var class="Arg">thread</var> )</td><td class="tdright">( function )</td></tr></table></div> |
| 80 | <p>The <code class="func">ThreadID</code> function returns a numeric thread id for the given thread. The thread id of the main thread is always 0.</p> |
| 81 | |
| 82 | |
| 83 | <div class="example"><pre> |
| 84 | <span class="GAPprompt">gap></span> <span class="GAPinput">CurrentThread();</span> |
| 85 | <thread #0: running> |
| 86 | <span class="GAPprompt">gap></span> <span class="GAPinput">ThreadID(CurrentThread());</span> |
| 87 | 0 |
| 88 | </pre></div> |
| 89 | |
| 90 | <p><a id="X787679BC7D36FAA3" name="X787679BC7D36FAA3"></a></p> |
| 91 | |
| 92 | <h5>6.1-5 KillThread</h5> |
| 93 | |
| 94 | <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ KillThread</code>( <var class="Arg">thread</var> )</td><td class="tdright">( function )</td></tr></table></div> |
| 95 | <p>The <code class="func">KillThread</code> function terminates the given thread. Any region locks that the thread currently holds will be unlocked. The thread can be specified as a thread object or via its numeric id.</p> |
| 96 | |
| 97 | <p>The implementation for <code class="func">KillThread</code> is dependent on the interpreter actually executing statements. Threads performing system calls, for example, will not be terminated until the system call returns. Similarly, long-running kernel functions will delay termination until the kernel function returns.</p> |
| 98 | |
| 99 | <p>Use of <code class="code">CALL_WITH_CATCH</code> will not prevent a thread from being terminated. If you wish to make sure that catch handlers will be visited, use <code class="func">InterruptThread</code> (<a href="chap6.html#X8521A5777CCD0B37"><span class="RefLink">6.1-8</span></a>) instead. <code class="func">KillThread</code> should be used for threads that cannot be controlled anymore in any other way but still eat system resources.</p> |
| 100 | |
| 101 | <p><a id="X7BC284107E56A01B" name="X7BC284107E56A01B"></a></p> |
| 102 | |
| 103 | <h5>6.1-6 PauseThread</h5> |
| 104 | |
| 105 | <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ PauseThread</code>( <var class="Arg">thread</var> )</td><td class="tdright">( function )</td></tr></table></div> |
| 106 | <p>The <code class="func">PauseThread</code> function suspends execution for the given thread. The thread can be specified as a thread object or via its numeric id.</p> |
| 107 | |
| 108 | <p>The implementation for <code class="func">PauseThread</code> is dependent on the interpreter actually executing statements. Threads performing system calls, for example, will not pause until the system call returns. Similarly, long-running kernel functions will not pause until the kernel function returns.</p> |
| 109 | |
| 110 | <p>While a thread is paused, the thread that initiated the pause can access the paused thread's thread-local region.</p> |
| 111 | |
| 112 | |
| 113 | <div class="example"><pre> |
| 114 | <span class="GAPprompt">gap></span> <span class="GAPinput">loop := function() while true do Sleep(1); od; end;;</span> |
| 115 | <span class="GAPprompt">gap></span> <span class="GAPinput">x := fail;;</span> |
| 116 | <span class="GAPprompt">gap></span> <span class="GAPinput">th := CreateThread(function() x := [1, 2, 3]; loop(); end);;</span> |
| 117 | <span class="GAPprompt">gap></span> <span class="GAPinput">PauseThread(th);</span> |
| 118 | <span class="GAPprompt">gap></span> <span class="GAPinput">x;</span> |
| 119 | [ 1, 2, 3 ] |
| 120 | </pre></div> |
| 121 | |
| 122 | <p><a id="X80D590EC78C93F94" name="X80D590EC78C93F94"></a></p> |
| 123 | |
| 124 | <h5>6.1-7 ResumeThread</h5> |
| 125 | |
| 126 | <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ ResumeThread</code>( <var class="Arg">thread</var> )</td><td class="tdright">( function )</td></tr></table></div> |
| 127 | <p>The <code class="func">ResumeThread</code> function resumes execution for the given thread that was paused with <code class="func">PauseThread</code> (<a href="chap6.html#X7BC284107E56A01B"><span class="RefLink">6.1-6</span></a>). The thread can be specified as a thread object or via its numeric id.</p> |
| 128 | |
| 129 | <p>If the thread isn't paused, <code class="func">ResumeThread</code> is a no-op.</p> |
| 130 | |
| 131 | <p><a id="X8521A5777CCD0B37" name="X8521A5777CCD0B37"></a></p> |
| 132 | |
| 133 | <h5>6.1-8 InterruptThread</h5> |
| 134 | |
| 135 | <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ InterruptThread</code>( <var class="Arg">thread</var>, <var class="Arg">interrupt</var> )</td><td class="tdright">( function )</td></tr></table></div> |
| 136 | <p>The <code class="func">InterruptThread</code> function calls an interrupt handler for the given thread. The thread can be specified as a thread object or via its numeric id. The interrupt is specified as an integer between 0 and <code class="func">MAX_INTERRUPT</code> (<a href="chap6.html#X863799F07D55BAE0"><span class="RefLink">6.1-11</span></a>).</p> |
| 137 | |
| 138 | <p>An interrupt number of zero (or an interrupt number for which no interrupt handler has been set up with <code class="func">SetInterruptHandler</code> (<a href="chap6.html#X80C207B387CA888E"><span class="RefLink">6.1-9</span></a>) will cause the thread to enter a break loop. Otherwise, the respective interrupt handler that has been created with <code class="func">SetInterruptHandler</code> (<a href="chap6.html#X80C207B387CA888E"><span class="RefLink">6.1-9</span></a>) will be called.</p> |
| 139 | |
| 140 | <p>The implementation for <code class="func">InterruptThread</code> is dependent on the interpreter actually executing statements. Threads performing system calls, for example, will not call interrupt handlers until the system call returns. Similarly, long-running kernel functions will delay invocation of the interrupt handler until the kernel function returns.</p> |
| 141 | |
| 142 | <p><a id="X80C207B387CA888E" name="X80C207B387CA888E"></a></p> |
| 143 | |
| 144 | <h5>6.1-9 SetInterruptHandler</h5> |
| 145 | |
| 146 | <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ SetInterruptHandler</code>( <var class="Arg">interrupt</var>, <var class="Arg">handler</var> )</td><td class="tdright">( function )</td></tr></table></div> |
| 147 | <p>The <code class="func">SetInterruptHandler</code> function allows the programmer to set up interrupt handlers for the current thread. The interrupt number must be in the range from 1 to <code class="func">MAX_INTERRUPT</code> (<a href="chap6.html#X863799F07D55BAE0"><span class="RefLink">6.1-11</span></a>) (inclusive); the handler must be a parameterless function (or <code class="keyw">fail</code> to remove a handler).</p> |
| 148 | |
| 149 | <p><a id="X78E5F5987BEFACE6" name="X78E5F5987BEFACE6"></a></p> |
| 150 | |
| 151 | <h5>6.1-10 NewInterruptID</h5> |
| 152 | |
| 153 | <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ NewInterruptID</code>( )</td><td class="tdright">( function )</td></tr></table></div> |
| 154 | <p>The <code class="func">NewInterruptID</code> function returns a previously unused number (starting at 1). These numbers can be used to globally coordinate interrupt numbers.</p> |
| 155 | |
| 156 | |
| 157 | <div class="example"><pre> |
| 158 | <span class="GAPprompt">gap></span> <span class="GAPinput">StopTaskInterrupt := NewInterruptID();</span> |
| 159 | 1 |
| 160 | <span class="GAPprompt">gap></span> <span class="GAPinput">SetInterruptHandler(StopTaskInterrupt, StopTaskHandler);</span> |
| 161 | </pre></div> |
| 162 | |
| 163 | <p><a id="X863799F07D55BAE0" name="X863799F07D55BAE0"></a></p> |
| 164 | |
| 165 | <h5>6.1-11 MAX_INTERRUPT</h5> |
| 166 | |
| 167 | <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ MAX_INTERRUPT</code></td><td class="tdright">( global variable )</td></tr></table></div> |
| 168 | <p>The global variable <code class="func">MAX_INTERRUPT</code> is an integer containing the maximum value for the interrupt arguments to <code class="func">InterruptThread</code> (<a href="chap6.html#X8521A5777CCD0B37"><span class="RefLink">6.1-8</span></a>) and <code class="func">SetInterruptHandler</code> (<a href="chap6.html#X80C207B387CA888E"><span class="RefLink">6.1-9</span></a>).</p> |
| 169 | |
| 170 | |
| 171 | <div class="chlinkprevnextbot"> <a href="chap0.html">[Top of Book]</a> <a href="chap0.html#contents">[Contents]</a> <a href="chap5.html">[Previous Chapter]</a> <a href="chap7.html">[Next Chapter]</a> </div> |
| 172 | |
| 173 | |
| 174 | <div class="chlinkbot"><span class="chlink1">Goto Chapter: </span><a href="chap0.html">Top</a> <a href="chap1.html">1</a> <a href="chap2.html">2</a> <a href="chap3.html">3</a> <a href="chap4.html">4</a> <a href="chap5.html">5</a> <a href="chap6.html">6</a> <a href="chap7.html">7</a> <a href="chap8.html">8</a> <a href="chap9.html">9</a> <a href="chap10.html">10</a> <a href="chap11.html">11</a> <a href="chapInd.html">Ind</a> </div> |
| 175 | |
| 176 | <hr /> |
| 177 | <p class="foot">generated by <a href="https://www.math.rwth-aachen.de/~Frank.Luebeck/GAPDoc">GAPDoc2HTML</a></p> |
| 178 | </body> |
| 179 | </html> |