Goto Chapter: Top 1 2 3 4 5 6 7 8 9 10 11 Ind
 [Top of Book]  [Contents]   [Previous Chapter]   [Next Chapter] 

2 Variables in HPC-GAP
 2.1 Global variables
 2.2 Thread-local variables

2 Variables in HPC-GAP

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.

2.1 Global variables

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.

2.2 Thread-local variables

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.

gap> MakeThreadLocal("x");
gap> x := 1;;
gap> WaitTask(RunTask(function() x := 2; end));
gap> x;
1

As can be seen here, the assignment to x in a separate thread does not overwrite the value of x in the main thread.

2.2-1 MakeThreadLocal
‣ MakeThreadLocal( name )( function )

MakeThreadLocal makes the variable described by the string name a thread-local variable. It normally does not give it an initial value; either explicit per-thread assignment or a call to BindThreadLocal (2.2-2) or BindThreadLocalConstructor (2.2-3) to provide a default value is necessary.

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 BindThreadLocal (2.2-2) had been called with that value as its second argument.

2.2-2 BindThreadLocal
‣ BindThreadLocal( name, obj )( function )

BindThreadLocal gives the thread-local variable described by the string name the default value obj. The first time the thread-local variable is accessed in a thread thereafter, it will yield obj as its value if it hasn't been assigned a specific value yet.

2.2-3 BindThreadLocalConstructor
‣ BindThreadLocalConstructor( name, func )( function )

BindThreadLocal (2.2-2) gives the thread-local variable described by the string name the constructor func. The first time the thread-local variable is accessed in a thread thereafter, it will yield func() as its value if it hasn't been assigned a specific value yet.

2.2-4 ThreadVar
‣ ThreadVar( global variable )

All thread-local variables are stored in the thread-local record ThreadVar. Thus, if x is a thread-local variable, using ThreadVar.x is the same as using x.

 [Top of Book]  [Contents]   [Previous Chapter]   [Next Chapter] 
Goto Chapter: Top 1 2 3 4 5 6 7 8 9 10 11 Ind

generated by GAPDoc2HTML