I've managed to modify the data of a RwLock
by storing the data inside a Mutex
like:
struct SqliteConn(RwLock<HashMap<String, Mutex<Option<rusqlite::Connection>>>>);
The RwLock
provides me "read" access to its HashMap
, but, while "reading" the HashMap
I can change the values of the HashMap
after obtaining a lock
from the Mutex
.
From the perspective of the Mutex
this makes sense because a Mutex
provides "interior" mutability... But... from the perspective of the RwLock
this feels like unsafe mutability of its HashMap
values.
Which perspective is "right"?
In case it helps, my use case is each time my business logic runs I first prefill a HashMap
with empty (None
) SQLite connections from a single thread and then connect every single thread to a separate db based on some shard key. So the HashMap
is only ever written to from a single thread and never written to while being read from (no writer starvation possible). But many threads do simultaneously read the HashMap
when they then go to connect and do work on the databases. The Mutex
s for all the Connections get mutated simultaneously from multiple threads (only one thread should read/write a single Connection at a time though, but to be careful the Mutex
ensures no two threads ever try to access the same Connection).