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
proposal: sync: add Mutex.LockContext #40026
Comments
Personally I think the channel based mutex is a better choice here. |
If that's the case, I agree it's not a good idea to add it into Perhaps it'd be better as a new |
cc @bcmills who has thought about this a lot A package that wraps up some of the channel-based approaches sounds useful, although it could live outside the standard library. |
Agreed. What other patterns do you think could be in this package? Others that come to my mind require generics. |
Based on the discussion above, this proposal - add Mutex.LockContext - seems like a likely decline. |
Agreed. I've created https://github.com/nhooyr/ctxsync but haven't implemented it yet. Will close this for now. Feel free to watch the repo for updates. |
Retracted. |
I propose we add the following function to the sync package:
At the moment, you can mimic this your own mutex based on
make(chan struct{}, 1)
.Every lock would correspond to a select on the ctx done chan and a send on the mutex channel.
And an unlock would read the value out of the channel.
This works and is what I use in my websocket library but it might be more generally useful and deserve a spot in the standard library.
I've often found myself needing it when I want to acquire a resource but the resource may be acquired for somewhat long periods of time by another caller and so in order to keep my code responsive, I need any mutex
Lock
calls to respect the context.The text was updated successfully, but these errors were encountered: