Threads
The thread keyword spawns concurrent threads of execution.
Syntax
thread {
// runs in a separate thread
HeavyComputation();
}
Thread Semantics
- Each
threadblock 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
| Scenario | Use |
|---|---|
| Independent CPU computations | thread |
| Data-parallel loops | parallel for |
| GPU tensor operations | gpu { } |
| I/O-bound operations | async / await |