On Sun, Nov 28, 2021 at 4:19 PM Денис Мухортов <muhortovdeni...@gmail.com> wrote:
> Why are the values not incremented when declaring a structure with mutex > in goroutines? > https://go.dev/play/p/bPc1bg0AvJ4 > You have multiple problems: 1. You are passing `counter` to the goroutine as a value, making a copy 2. You have to call `wg.Add` *before* the `go` statement, otherwise it is possible that the loop finishes before any of the goroutines get scheduled, causing `wg.Wait` to immediately return 3. The playground likely doesn't actually let you consume 10s of CPU time. Normally, the emulated time of the playground means that sleeping for 10s is not a problem (as it doesn't *actually* sleep), but you are busy-looping, so you are actually taking up CPU time. Here is a fixed version: https://go.dev/play/p/EZwW7clnORm It still times out in the playground, though. > And what is the actual point in declaring something in anonymous > functions, if in my experience it always works without it? > I don't understand this question. > -- > 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/7f3aaf03-0705-4e19-b027-68764b185b4dn%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/7f3aaf03-0705-4e19-b027-68764b185b4dn%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. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfFLjbJKjYsJbkwGa7_p_i0UPO4-d1qvpnpzrC3%2Bi7aFgA%40mail.gmail.com.