指定された拡張子のファイルを検索して正規表現に一致するものを探すために、スレッドプールを利用するプログラムを作成しています。
私のスレッドプールは次のようになります。
for( int i = 0; i < _nThreads; ++i )
{
_threads.push_back( thread( &ThreadPool::GrepFunc, 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 );
}
}
}
}
}
}
}
基本的に、プログラムはユーザーから初期ディレクトリを受け取り、それとすべてのサブディレクトリを再帰的に検索して、拡張子に一致するファイルを探し、正規表現に一致するものを探します。_doneに達したときの停止ケースを判別する方法を理解するのに問題があります。スレッドに戻る前に、初期ディレクトリ内のすべてのディレクトリとファイルがスキャンされていること、および_tasks内のすべての項目が完了していることを確認する必要があります。どんな考えでも本当にありがたいです。