I am writing a program that must recursively look for the folder structure and would like to do this in parallel with several threads.
I already wrote a rather trivial synchronous method - first adding the root directory to the queue, then unloading the directory, executing the queues in its subdirectories, etc., until the queue becomes empty. I'll useConcurrentQueue<T>for my turn, but I already realized that my cycles will stop prematurely. The first thread will delete the root directory, and immediately every other thread will see that the queue is empty and exit, leaving the first thread as the only one. I would like each thread to loop until the queue is empty, and then wait for the other thread to select a few more directories and continue working. I need some kind of checkpoint in my loop, so that none of the threads will exit until each thread reaches the end of the loop, but I'm not sure if the best way to do this is without deadlock, when there really isn’t more directories process.
source
share