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
I sometimes find myself needing to check if a goroutine which executes a sync.Once.Do() has done so yet. This could be done with channels or a separate variable but that can introduce a lot of unnecessary boilerplate. Not to mention that this means we're storing the same state twice which, depending on the code, could possibly get out of sync (ironically, given the package we're dealing with).
The text was updated successfully, but these errors were encountered:
Personally I feel like sync.Once does one job. No need to have it do another job. The implementation is less than 100 lines of well commented code; it would be simple to copy it and modify it for your needs.
Example Code
This version waits for Do() to finish if it is currently executing:
Note: Not tested
This version returns true if Do() is currently executing (probably not what we want to do):
Note: Not tested
Reason
I sometimes find myself needing to check if a goroutine which executes a
sync.Once.Do()
has done so yet. This could be done with channels or a separate variable but that can introduce a lot of unnecessary boilerplate. Not to mention that this means we're storing the same state twice which, depending on the code, could possibly get out of sync (ironically, given the package we're dealing with).The text was updated successfully, but these errors were encountered: