简体   繁体   中英

Using all available threads bad practice?

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,

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM