goroutine

Wait for the termination of n goroutines

坚强是说给别人听的谎言 提交于 2019-12-09 05:22:57
问题 I need to start a huge amount of goroutines and wait for their termination. The intuitive way seems to use a channel to wait till all of them are finished : package main type Object struct { //data } func (obj *Object) Update(channel chan int) { //update data channel <- 1 return } func main() { channel := make(chan int, n) list := make([]Object, n, m) for { for _, object := range list { go object.Update(channel) } for i := 0; i < n; i++ { <-channel } //now everything has been updated. start

Printing to stdout causes blocked goroutine to run?

半世苍凉 提交于 2019-12-08 19:23:22
问题 As a silly basic threading exercise, I've been trying to implement the sleeping barber problem in golang. With channels this should be quite easy, but I've run into a heisenbug. That is, when I try to diagnose it, the problem disappears! Consider the following. The main() function pushes integers (or "customers") onto the shop channel. barber() reads the shop channel to cut "customers'" hair. If I insert a fmt.Print statement into the customer() function, the program runs as expected.

Vectorise a function taking advantage of concurrency

牧云@^-^@ 提交于 2019-12-08 12:15:18
问题 For a simple neural network I want to apply a function to all the values of a gonum VecDense . Gonum has an Apply method for Dense matrices, but not for vectors, so I am doing this by hand: func sigmoid(z float64) float64 { return 1.0 / (1.0 + math.Exp(-z)) } func vSigmoid(zs *mat.VecDense) { for i := 0; i < zs.Len(); i++ { zs.SetVec(i, sigmoid(zs.AtVec(i))) } } This seems to be an obvious target for concurrent execution, so I tried var wg sync.WaitGroup func sigmoid(z float64) float64 { wg

How to stop goroutine blocked by external I/O started for process?

时光毁灭记忆、已成空白 提交于 2019-12-08 06:01:16
问题 I'm having a problem here that I can't exit goroutine safely. I'm having an external process created using exec.Command (storing cmd, stdin pipe and stdout pipe of the process): exec.Command(args[0], args[1]...) // args[0] is a base command Whenever there is a need to start that process I'm calling: cmd.Start() Then upon start and attaching I'm running 2 goroutines: shutdown := make(chan struct{}) // Run the routine which will read from process and send the data to CmdIn channel go pr

differences between pointer and value slice in for-range loop

淺唱寂寞╮ 提交于 2019-12-07 15:02:10
问题 Please check this code snippet: package main import ( "fmt" "time" ) type field struct { name string } func (p *field) print() { fmt.Println(p.name) } func main() { fmt.Println("use values:") // use values in range loop and go rountines values := []field{{"one"},{"two"},{"three"}} for _, v := range values { go v.print() } time.Sleep(time.Second) fmt.Println() fmt.Println("use pointers:") // use pointers in range loop and go rountines poniters := []*field{{"one"},{"two"},{"three"}} for _, v :=

How to kill goroutines? [duplicate]

半世苍凉 提交于 2019-12-07 14:10:09
问题 This question already has answers here : cancel a blocking operation in Go (2 answers) Closed last year . I would like to know how to kill/stop goroutines. All examples are based on channels and select, which seems to only work if the goroutine contains some repeating task between which it can listen on a channel. Is there a way to stop the below goroutine before it returns? package main import ( "time" ) func main() { stop := make(chan string, 1) go func() { time.Sleep(10 * time.Second) stop

GoLang: Decompress bz2 in on goroutine, consume in other goroutine

允我心安 提交于 2019-12-07 13:13:52
问题 I am a new-grad SWE learning Go (and loving it). I am building a parser for Wikipedia dump files - basically a huge bzip2-compressed XML file (~50GB uncompressed). I want to do both streaming decompression and parsing, which sounds simple enough. For decompression, I do: inputFilePath := flag.Arg(0) inputReader := bzip2.NewReader(inputFile) And then pass the reader to the XML parser: decoder := xml.NewDecoder(inputFile) However, since both decompressing and parsing are expensive operations, I

count / display the number of active goroutines

随声附和 提交于 2019-12-06 20:14:53
问题 I have a queue and a function that does both dequeueing and enqueueing. I want to make sure that the right amount of goroutines operate on the queue, as long as there is something in the list. This is the code I am using, but I was wondering if there is a way of printing the amount of currently active goroutines Link to playground var element int func deen(queue chan int) { element := <-queue fmt.Println("element is ", element) if element%2 == 0 { fmt.Println("new element is ", element) queue

why this code about golang goruntine running order is “2” first

梦想的初衷 提交于 2019-12-06 13:41:45
问题 package main import ( "fmt" "sync" ) func main() { runtime.GOMAXPROCS(1) w := &sync.WaitGroup{} w.Add(2) go func() { fmt.Println("1") w.Done() }() go func() { fmt.Println("2") w.Done() }() w.Wait() } https://play.golang.org/p/ESi1mKAo1x_S eh,I don't know why the "2" is print first. I want to check the information.But I don't know what's information should I check.So I post the question there for help. I think the first goroutine is the first one push in queue。The it should be print first. 回答1

GoLang: Decompress bz2 in on goroutine, consume in other goroutine

故事扮演 提交于 2019-12-05 20:30:19
I am a new-grad SWE learning Go (and loving it). I am building a parser for Wikipedia dump files - basically a huge bzip2-compressed XML file (~50GB uncompressed). I want to do both streaming decompression and parsing, which sounds simple enough. For decompression, I do: inputFilePath := flag.Arg(0) inputReader := bzip2.NewReader(inputFile) And then pass the reader to the XML parser: decoder := xml.NewDecoder(inputFile) However, since both decompressing and parsing are expensive operations, I would like to have them run on separate Go routines to make use of additional cores. How would I go