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: Condition Variables #2531
Comments
Could you please elaborate on the use-case of such primitive in coroutines? E.g. what domain-specific problem does it solve that cannot be solved otherwise? In general, for any kind of notifications channels or flows are preferred and can be used instead. Apart from that, coroutines are phantom, so one cannot ensure whether the current coroutine holds the lock. E.g. in your implementation, it's possible that one thread/coroutine acquires the lock, while the other awaits on its condition without actually holding the lock. Providing primitive that may be inconsistent is a time-bomb waiting to explode and we'd very much like to avoid it |
Hello,
There is no use-case that cannot be solved otherwise, since is it possible to implement Conditions from other primitives 😅 However it is perfectly suited anywhere you need to A few examples on how I'm using on my current project: Waiting for Debugging sessionMy current project involves network traffic, and I integrated it with a Wireshark capture plugin. This is a good/simple use-case. The (much-simplified) code looks like this:
Sparse data bufferMy project also deals with data transfers, and chunks of data can arrive out of order (similar to bittorrent). However, when consuming data, the reader must wait until it is available
Handling timeoutsA more complicated example: my project also keeps track of operations that can timeout, and specific events may extended the timeout (e.g., heartbeat messages) A simple solution which I used initially only relies on
However, while I only have a small-ish number of operations, I call My solution was to write my own scheduler, which stores all timeouts in a Heap and uses conditions to await until the next timeout/queue update. Timeout updates are very efficient (O(log(N)), cause no allocations, and only rarely wakes the scheduler
(Again, both snippets are much simplified)
I'm not sure what you mean here. Just like you can call mutex.lock() in a coroutine and The contract is that the mutex should be locked when Having another coroutine acquire the mutex while |
Thanks for a detailed explanation! Indeed these usages can be abstracted away, even in more idiomatic way (but it's a matter of style and I don't encourage you to rewrite it of course).
Indeed, though thread-obliviousness is a much less harmful property here. I'll keep this issue open for a while in case there are more compelling use-cases, but otherwise, benefits of conditional variable don't seem to outweigh its cons |
Condition Variables are an useful concurrency primitive, which I have been missing since starting with Kotlin Coroutines
Although it has not been optimised, I have written a very simple implementation and tests here
The text was updated successfully, but these errors were encountered: