Skip to main content

gosched

Function gosched 

Source
pub fn gosched()
Expand description

Yield the CPU, giving other goroutines a chance to run.

Moves the current goroutine to the back of the global run queue and re-enters the scheduler. Execution resumes at the next gosched() call site once the goroutine is rescheduled.

CPU-bound loops should call gosched() periodically. The background sysmon thread also sets a preemption hint after 10 ms, but because v1 has no stack-check traps the goroutine must call gosched() voluntarily for the hint to take effect.

§Panics

Panics if called from outside a goroutine (e.g. from main before calling run).

§Example

go_lib::run(|| {
    for i in 0..1_000_000 {
        if i % 10_000 == 0 {
            go_lib::gosched(); // let other goroutines run
        }
    }
});