Answer the question
In order to leave comments, you need to log in
How to limit the number of threads?
There is a task to count the number of words in all text files in a given directory, using 4 threads.
Wrote the solution in single-threaded mode.
I think to use ThreadPool. I want to know if there is a better way than ThreadPool?
Answer the question
In order to leave comments, you need to log in
#incude <omp.h>
void func()
{
std::vector<std::string> allFileNames;
// заполнить вектор путями всех нужных файлов в директории
#pragma omp parallel for num_threads(4)
for( auto & fileName : allFileNames )
{
// делать всякое
#pragma omp critical
{
// тут делать всякое без конфликтов между потоками
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question