5

I recently started working with C++ again and wrote a simple test application that finds the best path through a matrix of integer values. To improve the performance of this application I implemented multi-threading using C++11 std::thread.

unsigned int threadCount = std::thread::hardware_concurrency();
std::vector<std::thread> threads;
for (unsigned int threadIndex = 0; threadIndex < threadCount; threadIndex++) {
        threads.push_back(std::thread(&AltitudeMapPath::bestPath, this, threadCount, threadIndex));
}

for (auto& thread : threads) {
    thread.join();
}

As of right now I simply determine the total number of available threads and execute my test for each thread. This has worked fantastic but it got me thinking...

Is it bad practice to try and use all available threads for a given system? Beyond just this simple example do production level applications, that are multi-threaded, try to grab as many threads as they can (or as the problem will allow) or should I not be so greedy?

Thanks,

3
  • I think this question is rather broad: sometimes you want a process to be greedy, but it is possible that you don't really care about the program's performance. Perhaps add a parameter to you program such that users can specify the number of threads themselves? Commented Sep 16, 2015 at 16:52
  • 1
    Don't forget to take a look at std::async. It gives you some more abstraction than raw threads. For real production code, I would use a task-based library, e.g. Intel TBB, Microsoft PPL or Apple GCD.
    – Jens
    Commented Sep 17, 2015 at 7:00
  • @Jens Good to know! Yeah right now this is just a little side project to get me back into C++... been messing around with multi-threading and CUDA programming. However, it is always good to think about the big picture! Commented Sep 17, 2015 at 15:18

1 Answer 1

6

I don't think there is a single correct best practice, how many cores an application should use depends on the user's preference. There will be cases where a user wants an application to run as quickly as possible, and there will be cases where a user would prefer to multitask and not have an application bog down the machine.

I faced a similar problem and decided to make the number of threads configurable so that the user could choose between speed and cpu resource availability. I can think of at least one application that uses a similar configuration, so I don't think it is uncommon to let the user choose.

If you are forced to pick for the user, I would suggest using the number of hardware cores - 1 to free up a thread for the user to perform other work on.

Also keep in mind that std::thread::hardware_concurrency() is intended to be a hint and is allowed to return 0 if it can't make a determination.

5
  • 1
    number of hardware cores - 1 is quite good in practice. Using all cores, most of time, is not good for the user. Commented Sep 16, 2015 at 16:56
  • That is a good point that it varies on the application/users needs. I like your suggestion about leaving at least one core for other work. I am assuming the OS is smart enough to know that if 8 threads are available and I "allocate" 6 then it will leave one core untouched? Commented Sep 16, 2015 at 16:57
  • @ductiletoaster Generally, both Linux and Windows swaps processing cores from time to time, for what I think to prevent overheating. Commented Sep 16, 2015 at 16:59
  • Also with regard to hardware_concurrency() I have other checks within the application but to simplify my example I left them out. However, it is an important point to make! Commented Sep 16, 2015 at 17:00
  • When I needed to do something similar (back in the days before std::thread, so I used the OS's thread functions), I'd utilize all the available cores but bump the priority down a tick to keep the computer responsive while using all the available spare CPU time. I don't see a way to do that with std::thread, though. (The user was able to limit the number of threads.) Using number_of_hardware_cores-1 fails if hardware_concurrency returns 0 or 1. Commented Sep 17, 2015 at 0:37

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.