| 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 8: Semaphores</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="chap8" 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="chap7.html">[Previous Chapter]</a> <a href="chap9.html">[Next Chapter]</a> </div> |
| 21 | |
| 22 | <p id="mathjaxlink" class="pcenter"><a href="chap8_mj.html">[MathJax on]</a></p> |
| 23 | <p><a id="X83F63F0F7827767E" name="X83F63F0F7827767E"></a></p> |
| 24 | <div class="ChapSects"><a href="chap8.html#X83F63F0F7827767E">8 <span class="Heading">Semaphores</span></a> |
| 25 | <div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap8.html#X83F63F0F7827767E">8.1 <span class="Heading">Semaphores</span></a> |
| 26 | </span> |
| 27 | <div class="ContSSBlock"> |
| 28 | <span class="ContSS"><br /><span class="nocss"> </span><a href="chap8.html#X7AC5500483465AE3">8.1-1 CreateSemaphore</a></span> |
| 29 | <span class="ContSS"><br /><span class="nocss"> </span><a href="chap8.html#X834E47327A3FC5A2">8.1-2 WaitSemaphore</a></span> |
| 30 | <span class="ContSS"><br /><span class="nocss"> </span><a href="chap8.html#X7F0C1F1F8540CF8C">8.1-3 SignalSemaphore</a></span> |
| 31 | <span class="ContSS"><br /><span class="nocss"> </span><a href="chap8.html#X83D868417F0069F0">8.1-4 <span class="Heading">Simulating locks</span></a> |
| 32 | </span> |
| 33 | </div></div> |
| 34 | </div> |
| 35 | |
| 36 | <h3>8 <span class="Heading">Semaphores</span></h3> |
| 37 | |
| 38 | <p><a id="X83F63F0F7827767E" name="X83F63F0F7827767E"></a></p> |
| 39 | |
| 40 | <h4>8.1 <span class="Heading">Semaphores</span></h4> |
| 41 | |
| 42 | <p>Semaphores are synchronized counters; they can also be used to simulate locks.</p> |
| 43 | |
| 44 | <p><a id="X7AC5500483465AE3" name="X7AC5500483465AE3"></a></p> |
| 45 | |
| 46 | <h5>8.1-1 CreateSemaphore</h5> |
| 47 | |
| 48 | <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ CreateSemaphore</code>( [<var class="Arg">value</var>] )</td><td class="tdright">( function )</td></tr></table></div> |
| 49 | <p>The function <code class="func">CreateSemaphore</code> takes an optional argument, which defaults to zero. It is the counter with which the semaphore is initialized.</p> |
| 50 | |
| 51 | |
| 52 | <div class="example"><pre> |
| 53 | <span class="GAPprompt">gap></span> <span class="GAPinput">sem := CreateSemaphore(1);</span> |
| 54 | <semaphore 0x1108e81c0: count = 1> |
| 55 | </pre></div> |
| 56 | |
| 57 | <p><a id="X834E47327A3FC5A2" name="X834E47327A3FC5A2"></a></p> |
| 58 | |
| 59 | <h5>8.1-2 WaitSemaphore</h5> |
| 60 | |
| 61 | <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ WaitSemaphore</code>( <var class="Arg">sem</var> )</td><td class="tdright">( function )</td></tr></table></div> |
| 62 | <p><code class="func">WaitSemaphore</code> receives a previously created semaphore as its argument. If the semaphore's counter is greater than zero, it decrements the counter and returns; if the counter is zero, it waits until another thread increases it via <code class="func">SignalSemaphore</code> (<a href="chap8.html#X7F0C1F1F8540CF8C"><span class="RefLink">8.1-3</span></a>), then decrements the counter and returns.</p> |
| 63 | |
| 64 | |
| 65 | <div class="example"><pre> |
| 66 | <span class="GAPprompt">gap></span> <span class="GAPinput">sem := CreateSemaphore(1);</span> |
| 67 | <semaphore 0x1108e81c0: count = 1> |
| 68 | <span class="GAPprompt">gap></span> <span class="GAPinput">WaitSemaphore(sem);</span> |
| 69 | <span class="GAPprompt">gap></span> <span class="GAPinput">sem;</span> |
| 70 | <semaphore 0x1108e81c0: count = 0> |
| 71 | </pre></div> |
| 72 | |
| 73 | <p><a id="X7F0C1F1F8540CF8C" name="X7F0C1F1F8540CF8C"></a></p> |
| 74 | |
| 75 | <h5>8.1-3 SignalSemaphore</h5> |
| 76 | |
| 77 | <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ SignalSemaphore</code>( <var class="Arg">sem</var> )</td><td class="tdright">( function )</td></tr></table></div> |
| 78 | <p><code class="func">SignalSemaphore</code> receives a previously created semaphore as its argument. It increments the semaphore's counter and returns.</p> |
| 79 | |
| 80 | |
| 81 | <div class="example"><pre> |
| 82 | <span class="GAPprompt">gap></span> <span class="GAPinput">sem := CreateSemaphore(1);</span> |
| 83 | <semaphore 0x1108e81c0: count = 1> |
| 84 | <span class="GAPprompt">gap></span> <span class="GAPinput">WaitSemaphore(sem);</span> |
| 85 | <span class="GAPprompt">gap></span> <span class="GAPinput">sem;</span> |
| 86 | <semaphore 0x1108e81c0: count = 0> |
| 87 | <span class="GAPprompt">gap></span> <span class="GAPinput">SignalSemaphore(sem);</span> |
| 88 | <span class="GAPprompt">gap></span> <span class="GAPinput">sem;</span> |
| 89 | <semaphore 0x1108e81c0: count = 1> |
| 90 | </pre></div> |
| 91 | |
| 92 | <p><a id="X83D868417F0069F0" name="X83D868417F0069F0"></a></p> |
| 93 | |
| 94 | <h5>8.1-4 <span class="Heading">Simulating locks</span></h5> |
| 95 | |
| 96 | <p>In order to use semaphores to simulate locks, create a semaphore with an initial value of 1. <code class="func">WaitSemaphore</code> (<a href="chap8.html#X834E47327A3FC5A2"><span class="RefLink">8.1-2</span></a>) is then equivalent to a lock operation, <code class="func">SignalSemaphore</code> (<a href="chap8.html#X7F0C1F1F8540CF8C"><span class="RefLink">8.1-3</span></a>) is equivalent to an unlock operation.</p> |
| 97 | |
| 98 | |
| 99 | <div class="chlinkprevnextbot"> <a href="chap0.html">[Top of Book]</a> <a href="chap0.html#contents">[Contents]</a> <a href="chap7.html">[Previous Chapter]</a> <a href="chap9.html">[Next Chapter]</a> </div> |
| 100 | |
| 101 | |
| 102 | <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> |
| 103 | |
| 104 | <hr /> |
| 105 | <p class="foot">generated by <a href="https://www.math.rwth-aachen.de/~Frank.Luebeck/GAPDoc">GAPDoc2HTML</a></p> |
| 106 | </body> |
| 107 | </html> |