Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8385451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:40:27+00:00 2026-06-09T17:40:27+00:00

I am writing a program that utilizes a thread pool in order to search

  • 0

I am writing a program that utilizes a thread pool in order to search through files of a specified extension for matches to a regex expression.

My thread pool looks like this:

for( int i = 0; i < _nThreads; ++i )
    {
            _threads.push_back( thread( &ThreadPool::GrepFunc, this ) );
    }

and the running function looks like this:

void ThreadPool::GrepFunc()
{
    // implement a barrier

while( !_done )
{
    while( !_tasks.empty() )
    {
        fs::path task;
        bool gotTask = false;
        {
            lock_guard<mutex> tl( _taskMutex );
            if( !_tasks.empty() )
            {
                task = _tasks.front();
                _tasks.pop();
                gotTask = true;
            }
        }

        if( gotTask )
        {
            if( std::tr2::sys::is_directory( task ) )
            {
                for( fs::directory_iterator dirIter( task ), endIter; dirIter != endIter; ++dirIter )
                {
                    if( fs::is_directory( dirIter->path() ) )
                    {
                        { lock_guard<mutex> tl( _taskMutex );
                        _tasks.push( dirIter->path() ); }
                    }
                    else
                    {
                        for( auto& e : _args.extensions() )
                        {
                            if( !dirIter->path().extension().compare( e ) )
                            {
                                SearchFile( dirIter->path() );
                            }
                        }
                    }
                }
            }
            else
            {
                for( auto& e : _args.extensions() )
                {
                    if( !task.extension().compare( e ) )
                    {
                        SearchFile( task );
                    }
                }
            }
        }
    }
}
}

Essentially the program receives an initial directory from the user and will recursively search through it and all sub directories for files matching the extension looking for regex matches. I am having trouble figuring out how to determine the stopping case for when _done has been reached. I need to ensure that all directories and files inside the initial directory have been scanned and also that all items inside of _tasks have been completed before I join the threads back. Any thoughts would really be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:40:29+00:00Added an answer on June 9, 2026 at 5:40 pm

    I’d suggest having one thread (possibly the same thread spawning the file-processing threads) dedicated to doing the recursive filesystem search for matching files; it can add the files into a work queue from which the file-searching threads can pick up work. You can use a condition variable to coordinate this.

    Coordinating shutdown is a little tricky, as you’ve found. After the filesystem-search thread has completed its search, it can set some “just finish what’s queued” flag visible to the worker threads then signal them all to wake up and try to process another file: if they find the file/work queue empty they exit. The filesystem-search thread then joins all workers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a program that needs to read in very large files (about
I'm writing a Java program that's supposed to be the GUI frontend that utilizes
I'm writing a program that will continuously process files placed into a hot folder.
I am writing a program that reads '.exe' files and stores their hex values
Currently I am writing a program that must iterate through and arraylist inside of
I am writing a program that runs through pintools, to perform dynamic taint analysis
I'm writing a program that show the thread list of all opened process. With
I am writing an Objective-C program that utilizes the garbage collector. I am getting
I am writing a program that needs to search a LARGE text document for
I am writing the C++/Native side of a text/VoIP program that utilizes JNI. Currently

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.