sleep
sleep bloqueia a thread atual pela quantidade solicitada de milissegundos.
Assinatura
sleep(ms: u64): voidUse sleep em programas de linha de comando, scripts simples e testes nos quais bloquear a thread atual é intencional. Em um programa assíncrono com uma única thread de trabalho, ele bloqueia todo o runtime.
O que continua durante a espera
O código abaixo de await sleepAsync(...) na mesma função espera o atraso terminar. Outras tasks prontas podem usar a thread enquanto a função atual está suspensa. No primeiro programa, sleep bloqueia a thread, então imprime first e depois second. No segundo programa, sleepAsync suspende a primeira task, então imprime second e depois first.
tyrus
import { sleep } from "tyrus:task";
function main(): void {
sleep(1000);
print("first"); // Prints first.
print("second"); // Prints second.
}tyrus
import { sleepAsync, spawn } from "tyrus:task";
async function first(): Promise<void> {
await sleepAsync(1000);
print("first"); // Prints second, after sleepAsync completes.
}
async function second(): Promise<void> {
print("second"); // Prints first, while the first task waits.
}
async function main(): Promise<void> {
const firstTask = spawn(first);
const secondTask = spawn(second);
await firstTask.join();
await secondTask.join();
}error
tyrus
import { sleep } from "tyrus:task";
function main(): void {
sleep(-1);
}text
error[E0308]: type mismatch for `argument 1 of `sleep``: expected `u64`, found `negative integer literal`
at /src/main.tyr:4:5
sleep(-1);
^^^^^
help: change the value or the annotation so their types match; convert numbers explicitly with `value as <type>`
error[E0706]: integer literal `-1` does not fit in `u64`, which holds values from 0 to 18446744073709551615
at /src/main.tyr:4:11
sleep(-1);
^^
help: the narrowest type that holds this value is `i8`; widen the type or change the value to fit