-
Notifications
You must be signed in to change notification settings - Fork 18k
runtime: golang scheduler is not preemptive - it's cooperative? #11462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It is by design. There are no plans to make the scheduler fully preemtive, in normal situations, this is not a problem. |
The traditional work around is to unroll the loop and add a Gosched. If On Mon, Jun 29, 2015 at 7:19 AM, Dave Cheney [email protected]
|
@GoranP, you can think of the Go scheduler as being partially preemptive. It's by no means fully cooperative, since user code generally has no control over scheduling points, but it's also not able to preempt at arbitrary points. Currently, the general rule is that it can preempt at function calls, since at that point there's very little state to save or restore, which makes preemption very fast and dramatically simplifies some aspects of garbage collection. Your loop happens to contain none of these controlled preemption points, which is why the scheduler can't preempt it, but as @davecheney mentioned, this is fairly uncommon in real code (though by no means unheard of). We would like to detect loops like this and insert preemption points, but that work just hasn't happened yet. The issue for tracking that work (or, at least, the desire to do it :) is #10958. Duplicate of #10958. |
Thx guys for clarification. |
If you run this code with only one go routine:
Scheduler is paralized, and Printf is NOT printed after 100ms as expected, but after all job is done in cpuintensive() go routine.
But if programmer insert
runtime.Gosched()
in intensive for loop of routine, cooperative scheduler works fine.Is this by design or are there plans to make Golang scheduler preemptive?
The text was updated successfully, but these errors were encountered: