@include <SharedVar>

The SharedVar class provides a way to create and manage shared primitive variables across multiple processes. This class is useful for scenarios in which we’re forking processes and need a shared variable between them.

Reads and writes are atomic and concurrency-safe.

The variable can be encrypted for security.

Internally, it uses the file system to store the variable.

Methods

@SharedVar.get

Scope: public

Gets the value of the shared variable.

Usage: @SharedVar.get

@SharedVar.set

Scope: public

Sets the value of the shared variable.

Usage: @SharedVar.set $value

Parameters

  • value
    • Type: primitive
    • The value to set for the shared variable.

@SharedVar.setEncrypted

Scope: public

Sets whether the object should be encrypted. Encrypting the object can mitigate unauthorized reads by other processes.

This cannot be changed after the object has been used.

Usage: @SharedVar.setEncrypted $value

Parameters

  • value
    • Type: primitive
    • ‘1’ or ‘true’ enables encryption, any other value disables it.

@SharedVar.lock

Scope: public

Locks the shared object for exclusive access.

Once locked, only the process which locked it can access the object until it is unlocked.

Attempts by other processes access the object while it is locked will block until it is unlocked.

Using this method (as opposed to the internal _lock method) sets the extended_lock flag to 1, indicating that the lock should not be dropped until explicitly unlocked. I.e., subsequent calls to _unlock (internal) will not release the lock until unlock (public) is called.

Usage: @SharedVar.lock

@SharedVar.unlock

Scope: public

Unlocks the shared object, allowing other processes to access it.

This also resets the extended_lock flag to 0.

Usage: @SharedVar.unlock