go - why the "infinite" for loop is not processed? -


i need wait until x.addr being updated seems loop not run. suspect due go scheduler , i'm wondering why works way or if there way can fix it(without channels).

package main  import "fmt" import "time"  type t struct {     addr *string }  func main() {      x := &t{}     go update(x)     x.addr == nil {         if x.addr != nil {             break         }     }     fmt.println("hello, playground") }  func update(x *t) {     time.sleep(2 * time.second)     y := ""     x.addr = &y } 

there 2 (three) problems code.

first, right there no point in loop @ give control scheduler , such can't execute update goroutine. fix can set gomaxprocs bigger 1 , multiple goroutines can run in parallel.

(however, won't pass x value update function means main goroutine never see update on x. fix problem have pass x pointer. obsolete op fixed code.)

finally, note have data race on addr not using atomic loads , stores.


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - How to Hide Date Menu from Datepicker in yii2 -