Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

How about change async client mutex from std::sync::mutex to tokio::sync::mutex? #251

Open
jokemanfire opened this issue Sep 25, 2024 · 5 comments

Comments

@jokemanfire
Copy link
Contributor

Which feature do you think can be improved?

streams need aync lock write,so use tokio to decide the schedule.

How can it be improved?
tokio::sync::mutex

Additional Information
..

Before raising this feature request

..

@wllenyj
Copy link
Collaborator

wllenyj commented Sep 26, 2024

In what case does tokio Mutex need to be used?
If there's no clear usage case, I'd recommend leaving it as is.

@jokemanfire
Copy link
Contributor Author

while async request and disconnect func , these two operations will change the hashmap streams . If requests occur much more one of them ,a thread will hold for long time. use tokio::sync::mutex will release the thread's cpu while there's no chance to lock the hashmap.

@wllenyj
Copy link
Collaborator

wllenyj commented Sep 27, 2024

hold for long time ?
Are you sure you have read the tokio mutex documentation which-kind-of-mutex-should-you-use
?

@jokemanfire
Copy link
Contributor Author

jokemanfire commented Sep 27, 2024

If I misunderstand . The tokio.sync:mutex may be greater for IO intensive ?
`The feature that the async mutex offers over the blocking mutex is the ability to keep it locked across an .await point. This makes the async mutex more expensive than the blocking mutex, so the blocking mutex should be preferred in the cases where it can be used. The primary use case for the async mutex is to provide shared mutable access to IO resources such as a database connection. If the value behind the mutex is just data, it’s usually appropriate to use a blocking mutex such as the one in the standard library or parking_lot.

Note that, although the compiler will not prevent the std Mutex from holding its guard across .await points in situations where the task is not movable between threads, this virtually never leads to correct concurrent code in practice as it can easily lead to deadlocks.`

I just think the in the async status ,the tokio::sync::mutex may be greater?

@jokemanfire
Copy link
Contributor Author

That's just my opinion may not correct .
In Tokio runtime, using asynchronous tokio:: sync:: Mutex may have slightly lower performance compared to synchronous std:: sync:: Mutex, but it is designed specifically for asynchronous environments and is more suitable for use in Tokio.
Usage scenario: std:: sync:: Mutex may cause worker threads of Tokio to block, thereby affecting the performance of the entire Tokio runtime. And TikTok:: sync:: Mutex is designed to avoid this situation and is more suitable for use in scenarios that require asynchronous operations.

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

No branches or pull requests

2 participants