Search code examples
c++multithreadingc++11pthreadsstdthread

C++ - Optimal number of threads for processing string


I have a std::string of length N and I want to insert all substrings of length K into a std::set container, using threads. How many std::thread or pthread_t objects should I use?

Consider N = 500,000 and K = 3.


Solution

  • Use a ThreadPool.

    It is pretty simple to use, you'll only have to include "ThreadPool.h" and you can set the maximum number of threads based on the number of cores available. Your code should contain the following snipet.

     int max_threads = std::thread::hardware_concurrency();
     ThreadPool pool(max_threads);
     auto result = pool.enqueue(func,params); 
    

    Here func is the function to be called, params are the parameters and the value returned will be stored in result.