You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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
The text was updated successfully, but these errors were encountered: