Go routine as realtime thread without preemption?

236 views
Skip to first unread message

TheDiveO

unread,
Jan 21, 2023, 5:06:36 PM1/21/23
to golang-nuts
When a go routine gets locked to an OS thread, does the Go runtime scheduler then stops interrupting this Go routine, so when this Go routine (or rather the thread) gets pinned to an exclusive CPU core can make use of real time priorities?

Ian Lance Taylor

unread,
Jan 21, 2023, 6:18:34 PM1/21/23
to TheDiveO, golang-nuts
On Sat, Jan 21, 2023 at 2:06 PM TheDiveO <harald....@gmx.net> wrote:
>
> When a go routine gets locked to an OS thread, does the Go runtime scheduler then stops interrupting this Go routine, so when this Go routine (or rather the thread) gets pinned to an exclusive CPU core can make use of real time priorities?

No. Using runtime.LockOSThread does not exempt the goroutine from GOMAXPROCS.

Ian

TheDiveO

unread,
Jan 22, 2023, 12:17:35 PM1/22/23
to golang-nuts
On Sunday, January 22, 2023 at 12:18:34 AM UTC+1 Ian Lance Taylor wrote:
Using runtime.LockOSThread does not exempt the goroutine from GOMAXPROCS.

Hi Ian, thank you very much for your answer! I'm afraid that I do not yet understand (due to my limited runtime knowledge)  how to conclude from GOMAXPROCS that LockOSThread does not exclude locked go routine. Yet I'm intrigued to learn more, so can you please elaborate more?

Thank you very much!
TheDiveO

Ian Lance Taylor

unread,
Jan 23, 2023, 1:16:57 AM1/23/23
to TheDiveO, golang-nuts
I'm sorry, I don't understand your question.

Your original question was whether the Go runtime will interrupt a
thread for which a goroutine has called runtime.LockOSThread. The
answer to that question is yes.

I don't know what you mean when you say that LockOSThread does not
exclude a locked goroutine.

Ian

TheDiveO

unread,
Jan 23, 2023, 2:47:53 AM1/23/23
to golang-nuts
> On Sunday, January 22, 2023 at 12:18:34 AM UTC+1 Ian Lance Taylor wrote:
> Using runtime.LockOSThread does not exempt the goroutine from GOMAXPROCS.

I was asking to elaborate more on this previous answer of yours, as I don'tunderstand yet how GOMAXPROCS relates to (preemptive?) go routine scheduling.

Cheers,
TheDiveO

Ian Lance Taylor

unread,
Jan 23, 2023, 4:59:06 PM1/23/23
to TheDiveO, golang-nuts
I see. GOMAXPROCS is the number of goroutines that may run
simultaneously. See https://pkg.go.dev/runtime#GOMAXPROCS . So if
GOMAXPROCS is set to 4, then up to 4 goroutines may be running at one
time. If there are more goroutines ready to run, they will block
until one of those 4 goroutines pauses for some reason. If the 4
goroutines keep running for long enough without pausing, the runtime
will interrupt one of them to give other goroutines a chance to run.
All of this is true whether or not any goroutines have called
runtime.LockOSThread. The LockOSThread function affects which thread
is used to run a goroutine. It does not otherwise affect the
scheduler.

Ian
Reply all
Reply to author
Forward
0 new messages