Hi!
I use the tokio::spawn and need to use Mutex, Condvar and Arc but tokio does not has the Condvar.
There is the async-std crate but Condvar is unstable.
Which crate is better to use in my case?
If you need an async
variant of CondVar
, maybe tokio::sync::Notify
or tokio::sync::watch
might be what you need?
The std::sync::Arc
can be used either in a sync or async context.
Note that sometimes you should prefer to use sync mutexes instead of async ones, even in an async
function. This depends on the particular use case. Refer to Tokio's Shared State Tutorial for some more information on that:
As a rule of thumb, using a synchronous mutex from within asynchronous code is fine as long as contention remains low and the lock is not held across calls to
.await
.
In my case i have 2 async fn - writer and checker. And let pause_flag = Mutex.
The writer checks pause_flag and in case if it is true, writer waits for the condvar with this mutex.
The checker checks some data and sets true to pause_flag and then notify_all with condvar.
The Notify is useless in my case.
You can definitely do that with Notify
(like this), but the easiest way to do it is to use a watch
channel instead. It lets you keep track of a value (in your case, a boolean) and be notified when it changes.
wow, really)) Thanks))
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.