Concurrency Masterclass

Deterministic Multi-Threading in Nyx

How to run heavy parallel workloads across 16 or 64 CPU cores with maximum throughput, while guaranteeing that every bug and execution step can be replayed 100% identically.

1. The Big Problem with Traditional Multi-Threading

In most programming languages (such as C++, Java, and Go), multi-threading is notorious for causing Heisenbugs — bugs that happen randomly once in 10,000 runs and disappear the moment you try to debug them.

🍕 The Kitchen Analogy: Why Traditional Threads Collide

Imagine 8 chefs working in a busy restaurant kitchen, but all 8 chefs are grabbing ingredients from the exact same shared mixing bowl simultaneously without talking to each other. If Chef A adds salt while Chef B is stirring, the final recipe is chaotic and impossible to reproduce. That is how traditional shared-memory threading works.

In Nyx, we solve this by giving every chef their own private preparation station (Isolated Memory Regions) and having them coordinate through an ordered ticket queue (Logical Sequencing).

2. How Nyx Makes Threading 100% Deterministic

1. Zero Shared Mutable Memory

Threads in Nyx run in separate, isolated memory bump frames. Thread 1 physically cannot overwrite Thread 2's data. Data races are mathematically eliminated.

2. Logical Sequence Ordering

Cross-thread messages are tagged with sequential tickets (Ticket #1, Ticket #2). When recording a trace, we log only the ticket arrival order for instant replay.

3. Structured Nurseries

Parallel tasks are bounded inside explicit scopes. A parent task never finishes until all child tasks finish, preventing rogue orphan threads.

4. Deterministic Virtual Clocks

Parallel tasks requiring timestamps or random numbers read from a deterministic seeded virtual clock, ensuring identical simulation results across machines.

3. Hands-On Coding in Nyx

A. Parallel Processing with Structured Nurseries

Here is how you process 1,000,000 records in parallel across all CPU cores with zero data races:

import std::concurrency::nursery
import std::collections::List

// Pure processing function with zero global side-effects
@pure
fn calculate_analytics(data_chunk: List<float>) -> float {
    var total = 0.0
    for value in data_chunk {
        total += value * 1.15
    }
    return total
}

fn main() {
    let dataset = List::range(0, 1000000)
    var results = List::new()

    // Launch structured parallel nursery across CPU cores
    nursery { |scope|
        for chunk in dataset.chunks(8) {
            scope.spawn(fn() {
                // Runs in thread-isolated bump region
                let sum = calculate_analytics(chunk)
                scope.send_result(sum)
            })
        }
    } // All parallel threads merge deterministically here!

    println("Parallel analytics successfully completed!")
}

B. Isolated Actors with Ordered Channel Sequencing

For long-running background services (like financial ledgers or game servers), Nyx uses isolated Actor channels:

import std::actors::Actor
import std::actors::Channel

struct TransactionMsg {
    id: int,
    account_id: string,
    amount: float
}

// Dedicated Ledger Actor with private memory
actor BankLedger {
    var balance: float = 0.0

    fn handle_deposit(msg: TransactionMsg) {
        this.balance += msg.amount
        println("Processed transaction #" + msg.id.to_string())
    }
}

4. Recording & Replaying Multi-Threaded Bugs

When a multi-threaded program encounters an edge-case crash on a user's machine, Nyx can record the session with sub-1% overhead and replay it locally:

# 1. Run multi-threaded program with input logging
$ nyx run server.nyx --threads 16 --record crash_session.nyxtrace

# 2. Replay the exact multi-threaded execution locally
$ nyx run server.nyx --replay crash_session.nyxtrace

# 3. Step backwards and forwards in time in the Athena Debugger
$ nyx debug server.nyx --replay crash_session.nyxtrace --visualize
⏱️ Read Replayability Guide ❓ Read Architecture FAQ