E
E
Elnurhan2020-08-14 11:52:34
C++ / C#
Elnurhan, 2020-08-14 11:52:34

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

1 answer(s)
M
maaGames, 2020-08-14
@Elnurhan

#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 question

Ask a Question

731 491 924 answers to any question