Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite loop - 22_go-routines/09_channels/00_unbuffered-channels-block #22

Open
herickson123 opened this issue Mar 16, 2017 · 0 comments

Comments

@herickson123
Copy link

herickson123 commented Mar 16, 2017

Hey Todd,

Great stuff on these Udemy vids and code, I'm learning a lot. One thing I noticed however was the code here uses a Sleep in main() to wait for the goroutines to finish. I'm fine with using Sleep for testing deadlocks and race conditions but I modified this to not used a Sleep and use either a WaitGroup or boolean channel semaphores. Ran into issues and couldn't understand with such a simple example why trying to implement these schemes failed and the Sleep would work. Finally realized the goroutine in the infinite loop just dies when the main thread is finished by the end of the Sleep.

I've attached a txt file of the main.go file since I can't attach *.go types. It's got code for using for loop or channel range and WaitGroup or semaphores. One thing I did find out while testing and not sure if you mentioned this in your videos or not but if you used a channel range YOU MUST EXPLICITLY CLOSE THE CHANNEL or it will hang at the range loop.

Somewhere I read that the channels don't consume system resources like a network connection or a file open so unless they are being consumed through a channel/range loop it's not always necessary to handle closing them, they just get eligible for garbage collection when going out of scope and having no references.

Hopefully, this makes sense and may provide a little cleaner implementation should someone else try to do what I did.

Thanks,

Howard

main.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant