None of the examples will produce a deadlock.
From the documentation:
insert()
: Inserts a key and a value into the map. Returns the old value associated with the key if there was one.
Locking behaviour: May deadlock if called when holding any sort of reference into the map.
iter()
: Creates an iterator over a DashMap yielding immutable references.
Locking behaviour: May deadlock if called when holding a mutable reference into the map.
get_mut()
: Get a mutable reference to an entry in the map
Locking behaviour: May deadlock if called when holding any sort of reference into the map.
Explanation:
In your first two examples, only iter()
is used, so as long as no other operations that take mutable references into the maps are used, there will be no deadlocks.
In your third example, DashMap b
is only ever used with iter()
again, so no deadlocks there. For DashMap a
there are two possible execution flows:
- If thread 1 reaches
a.iter()
first, then thread 2 will have to wait at a.get_mut(key)
, but once thread 1 has finished iterating over a
thread 2 will be able to continue, so there will be no deadlock.
- If thread 2 reaches
a.get_mut(key)
first, then thread 1 will have to wait at a.iter()
but once thread 2 has finished it will be able to continue and there will be no deadlock.
Additional questions:
Inserting into a DashMap when iterating over it in the same thread will produce a deadlock.
Example:
for v in a.iter() { // This takes a reference into `a`
a.insert(...) // Will deadlock because you hold a reference into `a`
}
Inserting into a DashMap from one thread while iterating over it in another will not produce a deadlock, one of them will just wait for the other.
Example:
//thread1
for v in a.iter(){ //If thread2 reaches `insert()` first, this will wait until it has finished.
xxx
}
//thread2
for i in 1..1000 {
a.insert(i, i); // If thread1 reaches `iter()` first, this will wait until it has finished.
}
It is important to note that in the last example and in the 3rd example from the question, insert()
and iter()
may not wait for the whole loop to complete before starting, instead the execution of the two loops will be interleaved but they will never execute at the exact same time.
a
, and then later thread1 may iter the value just inserted? – Helbonna