goroutine

fatal error: all goroutines are asleep - deadlock

拟墨画扇 提交于 2019-12-13 03:55:16
问题 I've the following Go code package main import ( "fmt" "math/rand" ) const ( ROCK int = iota PAPER SCISSORS ) type Choice struct { Who int //0 you 1 your opponent Guess int } //Win returns true if you win. func Win(you, he int) bool { ... } func Opponent(guess chan Choice, please chan struct{}) { for i := 0; i < 3; i++ { <-please choice := rand.Intn(3) who := 1 guess <- Choice{who, choice} please <- struct{}{} } } func GetChoice(you, he int) int { ... } var Cheat func(guess chan Choice) chan

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

ぃ、小莉子 提交于 2019-12-13 02:48:29
问题 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) } // How

How to kill blocking go routine

两盒软妹~` 提交于 2019-12-13 02:08:41
问题 So i'm trying to implement a go routine that simply listens on a Redis subscription (I use the Go-redis library for this) and then sends messages on a channel after it recieves/processes the redis messages. Something like this: func foo(redis *redis.Client, comm chan HandlerSignal) { ... for { msg, err := pubsub.ReceiveMessage() sig := HandlerSignal{msg} comm <- sig } } But I can't figure out the best way to tell the go routine to return when it is blocking and waiting for a redis message.

Go Programming Language Mutual Concurrent Execution

馋奶兔 提交于 2019-12-12 17:53:20
问题 I have two concurrent go routines like below, Routine 1{ routine procedure critical section{ } routine procedure } Routine 2{ routine procedure critical section{ } routine procedure } Is it possible by using some go inbuilt functions to implement critical section ? 回答1: Your question: I have N concurrent go routines (all more or less same purpose). Each of those have a critical section. Before entering critical section, each routine just do some message sending job. When it enters critical

go routine not collecting all objects from channel

南笙酒味 提交于 2019-12-12 17:00:31
问题 I have one go-routine to add the objects into channel and then I have 4 go-routines to process objects of channel. The processing is nothing but adding objects to an array. But at few times, the objects are missing from final array. So I am assuming that at some point channel stops to collect objects. I have following code: package main import ( "log" "sync" ) func main() { j := 0 for { if j == 10 { break } wg := sync.WaitGroup{} months := []string{"Jan", "Feb", "Mar", "Apr", "May", "Jun",

Golang, goroutines : panic: runtime error: invalid memory address

只谈情不闲聊 提交于 2019-12-12 12:24:58
问题 I'm rather new in golang, and trying to understand main principles and write gouroutines based code, using chanels. In others langs that i was using there were no such instruments, and i wonder getting such an errors like panic... my code: package main import "fmt" import ( "time" ) type Work struct { x,y,z int } func worker(in <-chan *Work, out chan<- *Work){ for w := range in { w.z = w.x + w.y time.Sleep(time.Duration(w.z)) out <-w } } func sendWork(in chan <- *Work){ var wo *Work wo.x, wo

Limiting number of go routines running

时光怂恿深爱的人放手 提交于 2019-12-12 11:08:11
问题 I have a list of urls to process, but I want to run a maximum number of goroutines at a time. For example, if I have 30 urls, I only want 10 goroutines working in parallel. My attempt at this is the following: parallel := flag.Int("parallel", 10, "max parallel requests allowed") flag.Parse() urls := flag.Args() var wg sync.WaitGroup client := rest.Client{} results := make(chan string, *parallel) for _, url := range urls { wg.Add(1) go worker(url, client, results, &wg) } for res := range

How does Go decide when to context switch between goroutines?

落爺英雄遲暮 提交于 2019-12-12 09:39:48
问题 I am curious as to how the Go language schedules goroutines. Does it switch only during channel requests and I/O or does it have a periodic coroutine switching loop? 回答1: Go does not have a preemptive scheduler yet, but one is planned for 1.2. So no, Go will not switch context during CPU-only calculations, only during I/O (reading from memory is also considered I/O if it isn't in a register already). You can read some discussion about it in Issue 543 - preemptive scheduling. 来源: https:/

Golang: Why os.Exit doesn't work inside goroutines

泄露秘密 提交于 2019-12-12 08:46:23
问题 I have a research program with very simple algorithm. When success is coming goroutine should be close (end) via os.Exit(0). I'm wait one day, two day.... What? :) Here is the simple code package main import "os" func main() { for { go func() { os.Exit(0) }() } } And my questions: Why os.Exit doesn't terminate the goroutine? What is correct way to terminate (stop) goroutine execute? Playground: http://play.golang.org/p/GAeOI-1Ksc 回答1: You've run into a sticky corner of the Go scheduler. The

All goroutines are asleep - deadlock! ----— Error

十年热恋 提交于 2019-12-12 08:16:18
问题 I want to write three concurrent go routines that sends integers to each other. Now, my code is compiled properly, however after first execution it gives error "all goroutines are asleep - deadlock!". I tried to find the error but I could not able to find any error in code logic.Can anybody help me to find the mistake with my code. My code is given below. Thanks in advance. package main import "rand" func Routine1(command12 chan int, response12 chan int, command13 chan int, response13 chan