0

I am trying to create program that last 100 seconds. This program will create a thread every 2 milliseconds interval. Each thread will do a job that takes say 20 ms to complete.

So ideally there will be around 10 threads running at any point in time. How should I approach this problem?

#include <thread>

void runJob(); // took ~20 ms to complete

for (int i = 0; i < 50000; i++) {

    //create thread
    std::thread threadObj1(runJob);

    Sleep(2); 

};
4
  • Does this answer your question? Thread pooling in C++11 Commented Apr 30, 2020 at 11:55
  • what's the OS you're running on? Commented Apr 30, 2020 at 11:56
  • Can also consider using OpenMP en.wikipedia.org/wiki/OpenMP Commented Apr 30, 2020 at 12:00
  • Note that you need to join or detach your threads, otherwise if they take longer than the Sleep ~thread will call std::terminate Commented Apr 30, 2020 at 14:43

1 Answer 1

3

The problem with this approach is that with only 20ms worth of computation for each thread, you are very likely to spend considerably more CPU time spawning and shutting down threads than doing the actual work in runJob.

On most operating systems, spawning threads is quite an expensive operation and can easily take several dozen milliseconds on its own. So for relatively short lived jobs like you have, it is much more desirable to reuse the same thread for multiple jobs. This has the additional benefit that you don't create more threads than your system can handle, that is you avoid thread oversubscription.

So a better approach would be to create an appropriate number of threads upfront and then schedule the different runJob instances to those existing threads. Since this can be quite challenging to do by hand with just the barebones C++11 thread library facilities, you should consider using a higher level facility for this. OpenMP, C++17 parallel algorithms and task-based parallelization libraries like Intel Threading Building Blocks are all good options to get something like this off the ground quickly.

Sign up to request clarification or add additional context in comments.

1 Comment

Re, "...this can be quite challenging to do by hand..." You're talking about a thread pool. If you follow the link in the comment that @Botje posted on the original question, you can see a thread pool implementation in less than two dozen lines of code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.