Is it safe to .clone()
across threads?
#124
-
Let's say I have connections in deadpool. let pool: Pool<AsyncPgConnection> = Pool::builder(deadpool_config)
.max_size(deadpool_max_size)
.build()
.unwrap(); And I have to share the pool across multiple arbitrary number of async threads. // share the pool with async thread
let _pool = pool.clone();
tokio::spawn(async move {
let mut conn = _pool.get().await.unwrap();
// ... using conn
});
// and another different async thread
let _pool = pool.clone();
tokio::spawn(async move {
let mut conn = _pool.get().await.unwrap();
// ... using conn
});
// and so on...
// let _pool = pool.clone();
// tokio::spawn(async move { ....... I want the number of connections the entire rust program generates in total must be less than or equal to In the example above, I didn't wrapped the pool with Is this example suitable to satisfy my intention? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
To cite from the relevant documentation
So yes, it's safe to use it in that way. (Otherwise either the implementation would be unsound or the compiler would complain) |
Beta Was this translation helpful? Give feedback.
To cite from the relevant documentation
So yes, it's safe to use it in that way. (Otherwise either the implementation would be unsound or the compiler would complain)