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,
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.