bk0
bk0

Reputation: 1370

Does time.Sleep() yield to other goroutines?

In Go, does a call to time.Sleep() yield to other goroutines? I have a feeling it does, but in other answers (eg: Understanding goroutines) time.Sleep is not explicitly listed as a scheduling point.

Upvotes: 5

Views: 2085

Answers (1)

ferhatelmas
ferhatelmas

Reputation: 3978

Yes. See Pre-emption in the scheduler.

In prior releases, a goroutine that was looping forever could starve out other goroutines on the same thread, a serious problem when GOMAXPROCS provided only one user thread. In Go 1.2, this is partially addressed: The scheduler is invoked occasionally upon entry to a function. This means that any loop that includes a (non-inlined) function call can be pre-empted, allowing other goroutines to run on the same thread.

Following design docs are also good reads to learn more about scheduler:

Upvotes: 6

Related Questions