goroutine

Example for sync.WaitGroup correct?

穿精又带淫゛_ 提交于 2019-11-26 10:09:26
问题 Is this example usage of sync.WaitGroup correct? It gives the expected result, but I am unsure about the wg.Add(4) and the position of wg.Done() . Does it make sense to add the four goroutines at once with wg.Add() ? http://play.golang.org/p/ecvYHiie0P package main import ( \"fmt\" \"sync\" \"time\" ) func dosomething(millisecs time.Duration, wg *sync.WaitGroup) { duration := millisecs * time.Millisecond time.Sleep(duration) fmt.Println(\"Function in background, duration:\", duration) wg.Done

How to collect values from N goroutines executed in a specific order?

余生颓废 提交于 2019-11-26 08:26:39
问题 Below is a struct of type Stuff. It has three ints. A Number , its Double and its Power . Let\'s pretend that calculating the double and power of a given list of ints is an expensive computation. type Stuff struct { Number int Double int Power int } func main() { nums := []int{2, 3, 4} // given numbers stuff := []Stuff{} // struct of stuff with transformed ints double := make(chan int) power := make(chan int) for _, i := range nums { go doubleNumber(i, double) go powerNumber(i, power) } //

Is it safe to read a function pointer concurrently without a lock?

我的未来我决定 提交于 2019-11-26 05:38:28
问题 Suppose I have this: go func() { for range time.Tick(1 * time.Millisecond) { a, b = b, a } }() And elsewhere: i := a // <-- Is this safe? For this question, it\'s unimportant what the value of i is with respect to the original a or b . The only question is whether reading a is safe. That is, is it possible for a to be nil , partially assigned, invalid, undefined, ... anything other than a valid value? I\'ve tried to make it fail but so far it always succeeds (on my Mac). I haven\'t been able

cancel a blocking operation in Go

烈酒焚心 提交于 2019-11-26 04:56:36
问题 I have a blocking operation in a 3rd party library that I don\'t control. It could potentially go forever. So I want to set a timeout on it. The obvious way is to wrap it with a channel and a goroutine and then select on the result with time.After . However, the problem is the goroutine running the blocking operation could potentially go forever. Here is an example to illustrate this http://repl.it/90o Is there a way to cancel a goroutine or have it garbage collected? 回答1: You can't stop a

How to stop a goroutine

眉间皱痕 提交于 2019-11-26 02:42:43
问题 I have a goroutine that calls a method, and passes returned value on a channel: ch := make(chan int, 100) go func(){ for { ch <- do_stuff() } }() How do I stop such a goroutine? 回答1: EDIT: I wrote this answer up in haste, before realizing that your question is about sending values to a chan inside a goroutine. The approach below can be used either with an additional chan as suggested above, or using the fact that the chan you have already is bi-directional, you can use just the one... If your

Is this an idiomatic worker thread pool in Go?

跟風遠走 提交于 2019-11-26 02:04:48
I'm attempting to write a simple worker pool with goroutines. Is the code I wrote idiomatic? If not, then what should change? I want to be able to set the maximum number of worker threads to 5 and block until a worker becomes available if all 5 are busy. How would I extend this to only have a pool of 5 workers max? Do I spawn the static 5 goroutines, and give each the work_channel ? code: package main import ( "fmt" "math/rand" "sync" "time" ) func worker(id string, work string, o chan string, wg *sync.WaitGroup) { defer wg.Done() sleepMs := rand.Intn(1000) fmt.Printf("worker '%s' received: '

Is this an idiomatic worker thread pool in Go?

一个人想着一个人 提交于 2019-11-26 01:08:43
问题 I\'m attempting to write a simple worker pool with goroutines. Is the code I wrote idiomatic? If not, then what should change? I want to be able to set the maximum number of worker threads to 5 and block until a worker becomes available if all 5 are busy. How would I extend this to only have a pool of 5 workers max? Do I spawn the static 5 goroutines, and give each the work_channel ? code: package main import ( \"fmt\" \"math/rand\" \"sync\" \"time\" ) func worker(id string, work string, o