Skip to content
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

Any plan to support async? #11

Open
soulmachine opened this issue Mar 23, 2022 · 2 comments
Open

Any plan to support async? #11

soulmachine opened this issue Mar 23, 2022 · 2 comments

Comments

@soulmachine
Copy link

This is not a bug report but a feature request.

This library will expand its usage cases if it supports async. Nowadays a lot of users in Rust world are using tokio, I hope this libary can provide async APIs, they can co-exist with sync APIs.

@soulmachine
Copy link
Author

soulmachine commented Mar 23, 2022

Currently this library only provides blocking APIs, which makes it difficult to use in async environments.

For example, if I use lock.lock().unwrap(), it will block the current thread, while cooperative runtimes like tokio are not aware that it is a blocking API, tokio will not suspend this task and this thread might get occupied forever.

So I have to call lock.try_lock() periodically in a loop, to alleviate lock competing I make it sleep a random time between 10 to 100 milliseconds, see utils.rs#L489-L503

If this library provides an async version of lock(), I could just do the job by one line lock.lock_async().await;, that's it. When tokio sees await, it knows that it's a good timing to suspend current task and run other tasks, so the thread is not wasted.

@brunoczim
Copy link
Owner

I think this could be possible, but I am not sure about what API I'd use with non-Linux platforms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants