Open yjh0502 opened 6 years ago
As someone who has struggled plenty with deadlocks, I have often considered ways of implementing some sort of detection system. It wouldn't be too hard to add a feature-gated system to detect deadlocks caused by other Qutex
/QrwLocks
(as in your example) and I would be completely open to adding one. Most deadlocks, however, come from interactions with other, outside, queues and scheduling systems (threads and event loops). Developing a system to detect deadlocks caused by interactions with those systems would be quite an undertaking.
What I often do in my applications is to use a dependency graph to plan out ahead of time, in which order things will be enqueued. Here is a simple example of this. Here is a more complicated example which is rebuildable. If implemented correctly, using graphs like this to schedule tasks which require exclusive access to resources (usually a region of memory, aka. a buffer) will prevent deadlocks from happening in the first place.
I'm interested to hear any other ideas you may have as to what else could/should be done and how they could be implemented.
Here's example code which causes deadlock without unsafe code.
Is there any way to detect the deadlock? It would be nice for
FutureGuard
to returnDeadlocked
error (just likeCanceled
error) if there's any priorFutureGuard
which belongs to current task. In that case, above example will returnError(Deadlocked)
onf.wait()
instead of hanging on deadlock.