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
TheThread class is defined in thread.js and provides the core structure for managing thread execution:
thread.js
Thread Properties
name
name
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, Chefinstructions
instructions
An ordered array of instruction objects that define what the thread will execute. Each instruction has a
type and optional parameters.Example:pc (Program Counter)
pc (Program Counter)
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.state
state
The current execution state of the thread. See Thread States below for details.
blockedBy
blockedBy
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
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:nextInstruction()
Increments the program counter to move to the next instruction:Thread-Specific Properties
Scenarios can add custom properties to threads for scenario-specific behavior:Next Steps
Execution Engine
Learn how the engine executes threads step by step
Instruction Set
Explore all available instruction types