lib45d
45Drives C++ Library Development Documentation
Public Member Functions | Private Member Functions | Private Attributes | List of all members
ffd::MTDirCrawler Class Reference

Crawls through a directory with multiple worker threads, calling a calback function on each directory entry found. More...

#include <45d/MTDirCrawler.hpp>

Public Member Functions

 MTDirCrawler ()
 Construct a new MTDirCrawler object. More...
 
 ~MTDirCrawler ()=default
 Destroy the MTDirCrawler object. More...
 
void crawl (ffd_internal_fs::path base_path, std::function< bool(const ffd_internal_fs::directory_entry &)> callback, int threads)
 Kicks off thread workers and waits for them to finish. More...
 
void crawl_async (ffd_internal_fs::path base_path, std::function< bool(const ffd_internal_fs::directory_entry &)> callback, int threads)
 Kicks off thread workers. MTDirCrawler::wait() must be called at some point to join threads. More...
 
void wait (void)
 Wait for threads to finish. Must be called at some point after MTDirCrawler::crawl_async(). More...
 

Private Member Functions

void seed (const ffd_internal_fs::path &base_path)
 
void worker (std::function< bool(const ffd_internal_fs::directory_entry &)> callback)
 

Private Attributes

bool done_
 
std::deque< ffd_internal_fs::directory_entry > queue_
 
std::vector< std::thread > workers_
 
std::atomic< int > threads_running_
 
std::mutex mutex_
 
std::condition_variable cv_
 

Detailed Description

Crawls through a directory with multiple worker threads, calling a calback function on each directory entry found.

Constructor & Destructor Documentation

◆ MTDirCrawler()

ffd::MTDirCrawler::MTDirCrawler ( )
inline

Construct a new MTDirCrawler object.

◆ ~MTDirCrawler()

ffd::MTDirCrawler::~MTDirCrawler ( )
default

Destroy the MTDirCrawler object.

Member Function Documentation

◆ crawl()

void ffd::MTDirCrawler::crawl ( ffd_internal_fs::path  base_path,
std::function< bool(const ffd_internal_fs::directory_entry &)>  callback,
int  threads 
)
inline

Kicks off thread workers and waits for them to finish.

The callback parameter needs to be a function that receives a directory_entry and returns a boolean. Return true from this function if the crawler should recurse into this directory entry given that it is a directory itself. To pass more parameters to the callback function, use std::bind().

Example:

Parameters
base_pathPath to start the traversal from
callbackFunction to call on each directory entry, should return true if the directory entry should be recursed into
threadsNumber of worker threads to spawn

◆ crawl_async()

void ffd::MTDirCrawler::crawl_async ( ffd_internal_fs::path  base_path,
std::function< bool(const ffd_internal_fs::directory_entry &)>  callback,
int  threads 
)
inline

Kicks off thread workers. MTDirCrawler::wait() must be called at some point to join threads.

Parameters
base_pathPath to start the traversal from
callbackFunction to call on each directory entry, should return true if the directory entry should be recursed into
threadsNumber of worker threads to spawn

◆ wait()

void ffd::MTDirCrawler::wait ( void  )
inline

Wait for threads to finish. Must be called at some point after MTDirCrawler::crawl_async().


The documentation for this class was generated from the following file: