-
Notifications
You must be signed in to change notification settings - Fork 0
shared
ffredyk edited this page Jul 23, 2026
·
1 revision
Thread Safety
Declaration keyword — not a runtime command in the traditional sense.
shared _variableName = <initialValue>;
Declares a CAS-based atomic variable. Like private but creates a thread-safe variable backed by compare-and-swap operations. Shared variables can be safely read and modified from multiple schedulers without explicit locking.
Operations on shared variables — add, sub, get, set, compareSwap — are atomic and thread-safe.
The compiler emits special opcodes for shared variable declaration and access. The underlying storage uses Interlocked operations for atomicity. Shared variables are not full .NET object references — they wrap numeric values with CAS semantics.
shared _counter = 0;
shared _flag = false;
shared _total = 0.0;shared _counter = 0;
_counter add 1; // atomic — safe from any scheduler
_counter add 5; // atomic add_val = get _counter; // atomic readshared _lock = 0;
// Try to acquire "lock"
_success = _lock compareSwap [0, 1]; // true if acquired, false if already taken
if (_success) then {
// critical section
_lock set [nil, 0]; // release (NOT atomic — only one writer)
};shared _visitors = 0;
// Scheduler 1:
_visitors add 1;
// Scheduler 2:
_visitors add 1;
// Scheduler 3:
_total = get _visitors; // 2 (atomic read)- Shared values are numeric only (numbers and booleans).
-
seton shared is not atomic — usecompareSwapfor concurrent writes. - Not a replacement for full locking — use freeze/sendTo for complex data structures.
Fully thread-safe by design. All operations on shared variables are atomic or CAS-based.
- Getting Started
- Language Guide
- Type System
- Control Flow
- Functions & Code
- Strings & Text
- Arrays & Collections
- Concurrency
- Promise System
- Thread Safety
- Host API & Embedding
- CLI Tool
- Bytecode Reference
- Multiplayer
- Syntax Sugar
- Optimization Guide
- Benchmarks
- count
- select
- pushBack
- append
- deleteAt
- deleteRange
- resize
- reverse
- sort
- find
- in
- forEach
- freeze
- thaw
- isFrozen
- currentScheduler
- clientOwner
- allSchedulers
- schedulerName
- schedulerExists
- schedulerBudget
- setSchedulerBudget
- fiberCount
- readyFiberCount
- waitingFiberCount
- schedulerLoad
- sendTo