Goto Chapter: Top 1 2 3 4 5 6 7 8 9 10 11 Ind

### 9 Synchronization variables

#### 9.1 Synchronization variables

Synchronization variables (also often called dataflow variables in the literature) are variables that can be written only once; attempts to read the variable block until it has been written to.

Synchronization variables are created with CreateSyncVar (9.1-1), written with SyncWrite (9.1-2) and read with SyncRead (9.1-3).

gap> sv := CreateSyncVar();;
>      Sleep(10);
>      SyncWrite(sv, MakeImmutable([1, 2, 3]));
>    end);;
[ 1, 2, 3 ]


##### 9.1-1 CreateSyncVar
 ‣ CreateSyncVar( ) ( function )

The function CreateSyncVar takes no arguments. It returns a new synchronization variable. There is no need to deallocate it; the garbage collector will free the memory and all related resources when it is no longer accessible.

##### 9.1-2 SyncWrite
 ‣ SyncWrite( syncvar, obj ) ( function )

SyncWrite attempts to assign the value obj to syncvar. If syncvar has been previously assigned a value, the call will fail with a runtime error; otherwise, obj will be assigned to syncvar.

In order to make sure that the recipient can read the result, the obj argument should not be a thread-local object; it should be public, read-only, or shared.

 ‣ SyncRead( syncvar ) ( function )
SyncRead reads the value previously assigned to syncvar with SyncWrite (9.1-2). If no value has been assigned yet, it blocks. It returns the assigned value.