Most minimal example I could make https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=76c280d06791b537998620f3cca2db7a
Dependencies are crossbeam = "0.7.3" and parking_lot = "0.11.0".
Better run it in a new cargo project rather than the playground because the stdout formatting is messed up there.
The basic idea of this program is to simulate a simple p2p network. The first client already has the file (client.file
has a vec
filled with 1s). A new client joins every second with no file (client.file
has a vec
filled with 0s). Right after a new client is created, 2 threads are created for that client. The first thread (line 62-77) checks which parts of the client's file is 0 and puts a request in the queue for each. The second thread (line 80-97) takes requests out from the queue and changes those 0s into 1s.
The thread at the start (line 24-47) is for checking the progress and printing the output by looping over a vec containing all the clients, every 10ms.
If you run this you can see the percentages of each client keep increasing up to a point where it stops. This point varies from run to run. The intended behavior is for all clients to reach 100%. But this intended behavior has happened only a very few times. What could be causing the deadlock? And how do I stop that?