https://play.golang.org/p/clXDHmgT7RU
Please check out above code ,should i need to put digts function in mutex lock as variable number is shared between these goroutines .
No, not needed.
The reason because you’re using channels and delegation mechanism for both calcSquares
and calcCubes
. Each time any of them calls digits
asynchronously and they have a clear and dedicated channel to reply back. Also, in this case, channel makes more sense than mutex locking. Well done.
You only use mutex lock when a shared variable is read or is written by multiple processes. Example, go’s map
is not safe, so if you have multiple processes read/write against a map object, mutex makes more sense than channel for synchronizing map object.
Thanks for the reply… does it mean , in channels it never got conflicted.
Can’t conclude that 100% that it never got conflicted without your “concurrency” plan/mapping.
We can 100% say however, say that channel is meant for asynchronous messaging between processes function slightly different compared to mutex locking. So in your case, since you do not have any shared variables but passing the output here and there, channel is the best choice.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.