Skip to main content

Threads

The thread keyword spawns concurrent threads of execution.


Syntax

thread {
// runs in a separate thread
HeavyComputation();
}

Thread Semantics

  • Each thread block spawns a new OS-level thread
  • Execution continues immediately in the calling code
  • Use for CPU-bound parallel work across multiple cores

When to Use

ScenarioUse
Independent CPU computationsthread
Data-parallel loopsparallel for
GPU tensor operationsgpu { }
I/O-bound operationsasync / await

Next Steps