Re: [go-nuts] all goroutines are asleep - deadlock!

2022-03-25 Thread Tong Sun
Oh, thanks for the explain and pointing out `go vet` for future problems. 

Thanks!

On Friday, March 25, 2022 at 12:08:39 PM UTC-4 jake...@gmail.com wrote:

> The WaitGroup Documentation  says " A 
> WaitGroup must not be copied after first use.". 
>
> You are passing around and calling choppingActivity by value, so it is 
> being copied after Add() is called, and again each call to choppingAction() 
> and choppingSimulation(). 
>
> If you run "go vet" on your code it will alert you to the problems. 
>
> On Friday, March 25, 2022 at 10:27:02 AM UTC-4 sunto...@gmail.com wrote:
>
>> *Re-using the old thread for a new problem that I'm getting:*
>>
>> fatal error: all goroutines are asleep - deadlock!
>>
>> I rewrote my 
>> https://github.com/suntong/lang/blob/master/lang/Go/src/sys/butchers.go 
>> files from procedure based to OO based, as
>> https://github.com/suntong/lang/tree/master/lang/Go/src/sys/butchersOO
>>
>> The two programs behaves exactly the same, however my new "OO" approach 
>> ended with 
>>
>> fatal error: all goroutines are asleep - deadlock!
>> goroutine 1 [semacquire]:
>>
>> The only reason that I can think of is that,
>>
>> I changed my goroutine calling from function calling of
>> `go choppingProblem(i, knifeLeft, knifeRight)`
>> (
>> https://github.com/suntong/lang/blob/9057e5718e00d396d0fe9a232820bdb79a31df72/lang/Go/src/sys/butchers.go#L79-L82
>> )
>>
>> to method calling of
>> `go chopping.choppingAction(i, knifeLeft, knifeRight, )`
>> (
>> https://github.com/suntong/lang/blob/9057e5718e00d396d0fe9a232820bdb79a31df72/lang/Go/src/sys/butchersOO/main.go#L129-L132
>> )
>>
>> Might that be the reason? 
>> How to fix the problem?
>>
>> Thanks
>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/b3e5deba-8325-481e-9f5d-a115406da728n%40googlegroups.com.


Re: [go-nuts] all goroutines are asleep - deadlock!

2022-03-25 Thread jake...@gmail.com
The WaitGroup Documentation  says " A 
WaitGroup must not be copied after first use.". 

You are passing around and calling choppingActivity by value, so it is 
being copied after Add() is called, and again each call to choppingAction() 
and choppingSimulation(). 

If you run "go vet" on your code it will alert you to the problems. 

On Friday, March 25, 2022 at 10:27:02 AM UTC-4 sunto...@gmail.com wrote:

> *Re-using the old thread for a new problem that I'm getting:*
>
> fatal error: all goroutines are asleep - deadlock!
>
> I rewrote my 
> https://github.com/suntong/lang/blob/master/lang/Go/src/sys/butchers.go 
> files from procedure based to OO based, as
> https://github.com/suntong/lang/tree/master/lang/Go/src/sys/butchersOO
>
> The two programs behaves exactly the same, however my new "OO" approach 
> ended with 
>
> fatal error: all goroutines are asleep - deadlock!
> goroutine 1 [semacquire]:
>
> The only reason that I can think of is that,
>
> I changed my goroutine calling from function calling of
> `go choppingProblem(i, knifeLeft, knifeRight)`
> (
> https://github.com/suntong/lang/blob/9057e5718e00d396d0fe9a232820bdb79a31df72/lang/Go/src/sys/butchers.go#L79-L82
> )
>
> to method calling of
> `go chopping.choppingAction(i, knifeLeft, knifeRight, )`
> (
> https://github.com/suntong/lang/blob/9057e5718e00d396d0fe9a232820bdb79a31df72/lang/Go/src/sys/butchersOO/main.go#L129-L132
> )
>
> Might that be the reason? 
> How to fix the problem?
>
> Thanks
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/1e3d22ca-f4c0-4063-9fdf-6d6bdbcf3202n%40googlegroups.com.


Re: [go-nuts] all goroutines are asleep - deadlock!

2022-03-25 Thread Tong Sun
*Re-using the old thread for a new problem that I'm getting:*

fatal error: all goroutines are asleep - deadlock!

I rewrote my 
https://github.com/suntong/lang/blob/master/lang/Go/src/sys/butchers.go 
files from procedure based to OO based, as
https://github.com/suntong/lang/tree/master/lang/Go/src/sys/butchersOO

The two programs behaves exactly the same, however my new "OO" approach 
ended with 

fatal error: all goroutines are asleep - deadlock!
goroutine 1 [semacquire]:

The only reason that I can think of is that,

I changed my goroutine calling from function calling of
`go choppingProblem(i, knifeLeft, knifeRight)`
(https://github.com/suntong/lang/blob/9057e5718e00d396d0fe9a232820bdb79a31df72/lang/Go/src/sys/butchers.go#L79-L82)

to method calling of
`go chopping.choppingAction(i, knifeLeft, knifeRight, )`
(https://github.com/suntong/lang/blob/9057e5718e00d396d0fe9a232820bdb79a31df72/lang/Go/src/sys/butchersOO/main.go#L129-L132)

Might that be the reason? 
How to fix the problem?

Thanks


-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/beee5c10-9c8d-4c25-b418-ebe77349938fn%40googlegroups.com.


Re: [go-nuts] all goroutines are asleep - deadlock!

2017-08-07 Thread Konstantin Khomoutov
On Fri, Aug 04, 2017 at 08:11:45AM -0700, prankpla...@gmail.com wrote:

[...]
> func fact_worker(fact_resp_chan chan string, x int) {
> defer wg.Done()
> response := factorial(x)
> fact_resp_chan <-  fmt.Sprintf("Factorial of %d is : %d", x, response)
> }
[...]

Please also consider naming your identifiers consistently with what the
Go itself and the community uses — for instance, [1, 2].

That is, use factWorker and factRespChan.

"Snake case" (foo_bar) is not used in Go code.

1. https://golang.org/doc/effective_go.html#names
2. https://blog.golang.org/package-names

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] all goroutines are asleep - deadlock!

2017-08-04 Thread Shawn Milochik
On Fri, Aug 4, 2017 at 11:45 AM, Jannick Fahlbusch 
wrote:

> Hi,
>
> you need to spin up another goroutine which ranges over the channel:
> https://play.golang.org/p/OhLd6vOBYE
>
>
>
This has a race condition. There's no guarantee that all the lines will be
printed before main() exits. A better solution is to close the channel so
the range loop exits. I posted an example of that an hour ago:
https://play.golang.org/p/mKdzU1z6Tx

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] all goroutines are asleep - deadlock!

2017-08-04 Thread Jannick Fahlbusch
Hi,

you need to spin up another goroutine which ranges over the channel:
https://play.golang.org/p/OhLd6vOBYE

The reason for the deadlock is: You spin up N workers which are sending
the results over the "fact_chan"-Channel. After you did this, you are
listening on the main-routine for messages on fact_chan forever. But
after all of the workers finished their tasks, there is nobody to send
data. The runtime detects this and panics.

- Jannick

Am 04.08.2017 um 17:11 schrieb prankpla...@gmail.com:
> *Factorial Program in GoLang. Encountering deadlock.*
> *
> *
> *Hello All,*
> *
> *
> I am pretty new to GoLang , so please excuse me if question/doubt is silly. 
> 
> I am trying to write a Factorial program of numbers ranging from 0 to 5.
> My goal is to launch separate GO routines for each number and then
> collect o/p and print it. I have tries writing code which can be seen
> @ https://play.golang.org/p/MTzTuroxE5 (also below) .. but i am
> encountering DEADLOCK error. I have been trying to synchronize it but i
> have failed to do it so far. Please help me in understanding that how
> else i can write this program so that i properly close sending channel
> to avoid error.
> 
> --
> package main
> 
> import (
> "fmt"
> "sync"
> )
> 
> var wg sync.WaitGroup
> 
> func factorial(x int) int {
> if x > 0 {
> return x*factorial(x-1)
> }
> return 1
> }
> 
> func fact_worker(fact_resp_chan chan string, x int) {
> defer wg.Done()
> response := factorial(x)
> fact_resp_chan <-  fmt.Sprintf("Factorial of %d is : %d", x, response)
> }
> 
> 
> func main() {
> fact_chan := make(chan string)
> 
> for n := 0; n <=5 ; n++ {
> wg.Add(1)
> go fact_worker(fact_chan, n)
> }
> 
> 
> 
> for resp := range(fact_chan) {
> fmt.Printf("%s\n", resp)
> }
> wg.Wait()
> 
> }
> --
> 
> Thanks!
> 
> -- 
> You received this message because you are subscribed to the Google
> Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to golang-nuts+unsubscr...@googlegroups.com
> .
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.