Skip to content

runtime: golang scheduler is not preemptive - it's cooperative? #11462

Closed
@GoranP

Description

@GoranP

If you run this code with only one go routine:

package main

import "fmt"
import "runtime"
import "time"

func cpuIntensive(p *int) {
  for i := 1; i <= 100000000000; i++ {
    *p = i
  }
}

func main() {
  runtime.GOMAXPROCS(1)

  x := 0
  go cpuIntensive(&x)

  time.Sleep(100 * time.Millisecond)

  // printed only after cpuIntensive is completely finished
  fmt.Printf("x = %d.\n", x)
}

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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions