-4

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.

xennygrimmato
  • 2,646
  • 7
  • 25
  • 47
  • 1
    Probably as much as much core you have... but if you misuse multithreading it can actually slow down your program (at best case), so i suggest you to either learn alot about it, or stick to a single thread as long as you don't have a really good reason to use threads. – Melkon Aug 25 '15 at 06:15
  • If you want optimal efficiency, probably 1. – curiousguy Aug 25 '15 at 07:00
  • 1
    What locking method are you planning to use when writing to the `std::set`? You know `std::set` is not thread safe right? How frequently are you expecting to find duplicate substrings? – Chris Drew Aug 25 '15 at 07:13
  • My aim is to find the number of times duplicate strings occur, in a multi-threaded environment, over 100 different random strings. @ChrisDrew - I did not know that `std::set` is not thread safe. – xennygrimmato Aug 25 '15 at 07:24

1 Answers1

5

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.

Pooja Nilangekar
  • 1,419
  • 13
  • 20