Pregunta

There are two odd things.

  1. I made 1000 numbers in slice but it just print 246,why 246?and why not 1000?

  2. if I delete "log.Println("hey")"this line,why does it just print 0?

I know it may has sync problem,but i haven't write any concurrence programs before,so any article can recommend?

import (
      "log"
      "runtime"
)

func main() {

  count := 1000
  slice := make([] int,count)
  for i := 0; i <= count-1; i++ {
   slice[i] =i
  }
  for _,v := range slice{
    go echo(v)
  }
  log.Println("hey")//if delete this line,it just print 0
  runtime.Gosched()
}


func echo(v int) {
  log.Println(v)
}
¿Fue útil?

Solución

There's no guarantee that any of the go routines run before your main routine completes. When the main routine completes, your program exits without waiting for all the go routines you created to complete (or even start).

The easiest way to fix this is to allocate a synchronization channel, pass it to each echo instance, and write a token to it after your log statement. Then the main thread should read count tokens out of that channel before returning.

Otros consejos

If you exit your main go routine, it doesn't wait for any go routines that are already running. You need to synchronize the running go routines and in my experience a sync.WaitGroup is the right general solution.

Playground

import (
    "log"
    "sync"
)

func main() {

    count := 1000
    slice := make([]int, count)
    for i := 0; i <= count-1; i++ {
        slice[i] = i
    }
    wg := new(sync.WaitGroup)
    for _, v := range slice {
        wg.Add(1)
        go echo(v, wg)
    }
    wg.Wait()
}

func echo(v int, wg *sync.WaitGroup) {
    defer wg.Done()
    log.Println(v)
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top