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
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.
global Queue ( consists of folders in a drive/directory ) Local Queue ( inside Thread function for Subfolders )
while(true)
{
lock()
if( Global q empty)
unlock
break
else
get first folder from global q
put it in local q
unlock
while(local q not empty)
take first folder from local q
process it
if( folder)
put it in local q
else
put it in vector ( othr purpose)
}