Listing Files using Multithreading with windows API in c++

后端 未结 2 330
借酒劲吻你
借酒劲吻你 2021-01-26 17:06

I have written a code that takes directory as input and outputs list of files in it.I implemented it in single thread.what to do in order to implement using multiple threads? pr

2条回答
  •  梦毁少年i
    2021-01-26 17:54

    The first approach that comes to my mind would be to use producer/consumer pattern where your consumers are also producers. The idea is to map listing of files in a single directory (not recursively) to a task. These tasks can be processed in parallel.

    E.g. initially you have a single task - to process your given directory. When you find a subdirectory you add it to directories vector and so another thread from your pool waiting for work can take this task and process. So you need to transform you directories to producer/consumer queue and create pool of threads to process this queue. Every time you find a subdirectory add to that queue as you do right now.

    The only problem is to detect when you finished your work. You can use something like a counter for waiting threads that have no work to do. When that counter reaches number of threads in your pool - post terminating items to your queue, one item per thread, that clearly indicates that thread consumed this item should terminate.

提交回复
热议问题