Skip to main content

Overview

Threads are the fundamental execution units in the simulator. Each thread represents an independent sequence of instructions that executes step-by-step through the simulation engine.

Thread Class

The Thread class is defined in thread.js and provides the core structure for managing thread execution:
thread.js
export class Thread {
  constructor(name, instructions) {
    this.name = name; // Nombre visible en UI y logs.
    this.instructions = instructions; // Lista ordenada de instrucciones.
    this.pc = 0; // Indice de la instruccion actual.
    this.state = "ready"; // ready, running, blocked, finished.
    this.blockedBy = null; // Recurso que bloqueo el hilo.
  }

  // Devuelve la instruccion apuntada por el PC.
  currentInstruction() {
    return this.instructions[this.pc];
  }

  // Avanza el PC a la siguiente instruccion.
  nextInstruction() {
    this.pc++;
  }
}

Thread Properties

The thread’s identifier used in the UI and execution logs. This makes it easy to track which thread is performing which action.Example: Cliente-1, Trabajo-3, Chef
An ordered array of instruction objects that define what the thread will execute. Each instruction has a type and optional parameters.Example:
[
  { type: Instructions.ACQUIRE },
  { type: Instructions.WITHDRAW, amount: 100 },
  { type: Instructions.RELEASE },
  { type: Instructions.END }
]
An index pointing to the current instruction in the instructions array. Starts at 0 and increments as the thread executes.The program counter determines which instruction will be executed next when the engine processes this thread.
The current execution state of the thread. See Thread States below for details.
Reference to the synchronization primitive (mutex, semaphore, condition variable, etc.) that is blocking this thread. null if the thread is not blocked.

Thread States

Threads transition through four distinct states during execution:

ready

The thread is ready to execute and will run in the next simulation step.This is the initial state when a thread is created.

running

The thread is currently executing an instruction.Set by the engine during the execution cycle.

blocked

The thread is waiting for a resource and cannot proceed.Examples: waiting for a mutex, semaphore, or condition variable.

finished

The thread has completed all instructions.Threads in this state are skipped by the execution engine.

State Transitions

The execution engine manages thread state transitions during each simulation step:

Creating Threads

Threads are typically created in scenario setup functions. Here’s an example from the bank scenario:
bankScenario.js
import { Thread } from "../core/thread.js";
import { Instructions } from "../core/instructions.js";

for (let i = 1; i <= threadCount; i++) {
  const instructions = [
    { type: Instructions.ACQUIRE },
    { type: Instructions.WITHDRAW, amount: 100 },
    { type: Instructions.RELEASE },
    { type: Instructions.END },
  ];

  const thread = new Thread(`Cliente-${i}`, instructions);
  engine.addThread(thread);
}
Each thread maintains its own program counter and executes independently, allowing the simulator to demonstrate concurrent execution and synchronization challenges.

Thread Methods

currentInstruction()

Returns the instruction object at the current program counter position:
const inst = thread.currentInstruction();
console.log(inst); // { type: "acquire" }

nextInstruction()

Increments the program counter to move to the next instruction:
thread.pc; // 0
thread.nextInstruction();
thread.pc; // 1
The engine calls nextInstruction() automatically after successfully executing most instructions. You rarely need to call this manually.

Thread-Specific Properties

Scenarios can add custom properties to threads for scenario-specific behavior:
const thread = new Thread(`Trabajo-${i}`, instructions);
thread.jobPages = pages; // Track pages for metrics

Next Steps

Execution Engine

Learn how the engine executes threads step by step

Instruction Set

Explore all available instruction types