Is there anyway one kernel thread can switch between compute intensive tasks?

kant kodali kanth909 at gmail.com
Thu Jan 25 18:58:23 UTC 2018


Hi Alan,

Vow..you got it. Below is the Go code. Can you please explain a bit more on
how and why Kernel thread yields only for Printf? Does it not yield for any
other call? can we achieve the same using the Java code (just using kernel
threads) in my previous email? I am setting GOMAXPROCS=1 such that it is
equivalent to Java version.


package main

import (
   "fmt"
   "time"
   "runtime"
)

var tasks = 3

func main() {
   runtime.GOMAXPROCS(1)
   counters := make([]int, tasks)
   for i := 0; i < tasks; i++ {
      go func(counter *int) {
         for {
            (*counter)++
            fmt.Print("")
         }
      }(&counters[i])
   }
   for {
      time.Sleep(time.Second)
      fmt.Printf("%#v\n", counters)
   }
}




On Thu, Jan 25, 2018 at 6:30 AM, Alan Bateman <Alan.Bateman at oracle.com>
wrote:

> On 25/01/2018 09:51, kant kodali wrote:
>
> :
>
>
> If you actually run the program the count will only change for one job but
> not for all three jobs. But if I use green threads in Golang and assign
> each green thread an infinite loop they are able to switch however what I
> don't understand is that once a green thread assigns a kernel thread to run
> one infinite loop how come other green threads were able to schedule on the
> same kernel thread? can someone enlighten me here? Sorry if my question is
> naive.
>
> Is your equivalent in Go using Printf in the loop? I don't know the Go
> runtime but that is one possible place for a kernel thread to yield. There
> is also the GOMAXPROCS variable to control the number of kernel threads
> that run concurrently.
>
> -Alan.
>


More information about the loom-dev mailing list