To clarify, the inner Go routine calls Done() at the start, so the main can get to the Broadcast before the inners are waiting.
Which is what Jan’s fix will fix :) (pretty sure) > On Jul 18, 2024, at 10:20 AM, robert engels <reng...@ix.netcom.com> wrote: > > Because the wait on the condition is in another Go routine, so the the > Broadcast occurs before all of the routines are waiting. Broadcast only wakes > up currently waiting routines. > >> On Jul 18, 2024, at 9:01 AM, Lammie Jonson <jrubia...@gmail.com >> <mailto:jrubia...@gmail.com>> wrote: >> >> // I am not sure why this code sometimes deadlocks, but sometimes it doesn't. >> // Hopefully someone will have some suggestions >> >> package main >> >> // ??? sometimes deadlocks >> >> import ( >> "fmt" >> "sync" >> "time" >> ) >> >> type Button struct { >> Clicked *sync.Cond >> } >> >> func main() { >> button := Button{ >> Clicked: sync.NewCond(&sync.Mutex{}), >> } >> >> // running on goroutine every function that passed/registered >> // and wait, not exit until that goroutine is confirmed to be running >> subscribe := func(c *sync.Cond, param string, fn func(s string)) { >> var goroutineRunning sync.WaitGroup >> var cnt = 0 >> cnt = cnt + 1 >> >> fmt.Println("cnt:", cnt) >> goroutineRunning.Add(1) >> >> go func(p string) { >> goroutineRunning.Done() >> c.L.Lock() // critical section >> defer c.L.Unlock() >> >> fmt.Println("Registered and wait ... ") >> // HANG SPOT: can hang here >> c.Wait() >> fmt.Println("afer goroutine wait for:", param) >> >> fn(p) >> }(param) >> fmt.Println("call wait before goroutine exit") >> goroutineRunning.Wait() >> fmt.Println("after wait, subscribe exits") >> } >> >> var clickRegistered sync.WaitGroup >> >> for _, v := range []string{ >> "Maximizing window.", >> "Displaying annoying dialog box!", >> "Mouse clicked."} { >> >> clickRegistered.Add(1) >> >> subscribe(button.Clicked, v, func(s string) { >> fmt.Println(s, "****************************") >> clickRegistered.Done() >> }) >> } >> >> fmt.Println("broadcast") >> // is supposed to get the goroutines past HANG SPOT >> button.Clicked.Broadcast() >> time.Sleep(4000 * time.Millisecond) >> >> clickRegistered.Wait() >> } >> >> -- >> 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 >> <mailto:golang-nuts+unsubscr...@googlegroups.com>. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/d8148ef1-4d56-4b0b-a2d8-2992abe7a6c2n%40googlegroups.com >> >> <https://groups.google.com/d/msgid/golang-nuts/d8148ef1-4d56-4b0b-a2d8-2992abe7a6c2n%40googlegroups.com?utm_medium=email&utm_source=footer>. > > > -- > 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 > <mailto:golang-nuts+unsubscr...@googlegroups.com>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/BCE619ED-241A-4239-99E2-D8C20994EB0A%40ix.netcom.com > > <https://groups.google.com/d/msgid/golang-nuts/BCE619ED-241A-4239-99E2-D8C20994EB0A%40ix.netcom.com?utm_medium=email&utm_source=footer>. -- 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/68FB0F9A-E05F-4909-ABD6-13A05922DE86%40ix.netcom.com.