<h3>2 <span class="Heading">Variables in HPC-GAP</span></h3>
<p>Variables with global scope have revised semantics in HPC-GAP in order to address concurrency issues. The normal semantics of global variables that are only accessed by a single thread remain unaltered.</p>
<p>Global variables in HPC-GAP can be accessed by all threads concurrently without explicit synchronization. Concurrent access is safe, but it is not deterministic. If multiple threads attempt to modify the same global variable simultaneously, the resulting value of the variable is random; it will be one of the values assigned by a thread, but it is impossible to predict with certainty which specific one will be assigned.</p>
<p>HPC-GAP supports the notion of thread-local variables. Thread-local variables are (after being declared as such) accessed and modified like global variables. However, unlike global variables, each thread can assign a distinct value to a thread-local variable.</p>
<p>As can be seen here, the assignment to <code class="code">x</code> in a separate thread does not overwrite the value of <code class="code">x</code> in the main thread.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ MakeThreadLocal</code>( <var class="Arg">name</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p><code class="func">MakeThreadLocal</code> makes the variable described by the string <var class="Arg">name</var> a thread-local variable. It normally does not give it an initial value; either explicit per-thread assignment or a call to <code class="func">BindThreadLocal</code> (<a href="chap2.html#X81F4832C7ED44627"><span class="RefLink">2.2-2</span></a>) or <code class="func">BindThreadLocalConstructor</code> (<a href="chap2.html#X8606D69B82B8AE84"><span class="RefLink">2.2-3</span></a>) to provide a default value is necessary.</p>
<p>If a global variable with the same name exists and is bound at the time of the call, its value will be used as the default value as though <code class="func">BindThreadLocal</code> (<a href="chap2.html#X81F4832C7ED44627"><span class="RefLink">2.2-2</span></a>) had been called with that value as its second argument.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ BindThreadLocal</code>( <var class="Arg">name</var>, <var class="Arg">obj</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p><code class="func">BindThreadLocal</code> gives the thread-local variable described by the string <var class="Arg">name</var> the default value <var class="Arg">obj</var>. The first time the thread-local variable is accessed in a thread thereafter, it will yield <var class="Arg">obj</var> as its value if it hasn't been assigned a specific value yet.
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ BindThreadLocalConstructor</code>( <var class="Arg">name</var>, <var class="Arg">func</var> )</td><tdclass="tdright">( function )</td></tr></table></div>
<p><code class="func">BindThreadLocal</code> (<a href="chap2.html#X81F4832C7ED44627"><span class="RefLink">2.2-2</span></a>) gives the thread-local variable described by the string <var class="Arg">name</var> the constructor <var class="Arg">func</var>. The first time the thread-local variable is accessed in a thread thereafter, it will yield <var class="Arg">func()</var> as its value if it hasn't been assigned a specific value yet.
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ ThreadVar</code></td><td class="tdright">( global variable )</td></tr></table></div>
<p>All thread-local variables are stored in the thread-local record <code class="func">ThreadVar</code>. Thus, if <code class="code">x</code> is a thread-local variable, using <code class="code">ThreadVar.x</code> is the same as using <code class="code">x</code>.</p>
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.