You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When Pausing cpr::ThreadPool, all the created threads become hyper active because of yielding, the expected behavior is the created threads to use little to none of the CPU.
Submit some work to the thread pool so all the threads will be created
for(std::size_t i = 0; i < std::thread::hardware_concurrency(); ++i) {
tp.Submit([i]() -> void {
std::cout << "job #" << i << " started" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1)); // simulate actual work
std::cout << "job #" << i << " finished" << std::endl;
});
}
Pause the thread pool
tp.Pause();
// Hold main thread to keep thread pool running
std::cin.get();
Notice High CPU usage even after all jobs are completed; (Use htop or system monitor utility on linux, or task manager on windows..)
Complete example code:
#include<iostream>
#include<cpr/threadpool.h>intmain() {
cpr::ThreadPool tp;
tp.SetMinThreadNum(1);
tp.SetMaxThreadNum(std::thread::hardware_concurrency());
tp.Start(0);
for(std::size_t i = 0; i < std::thread::hardware_concurrency(); ++i) {
tp.Submit([i]() -> void {
std::cout << "job #" << i << " started" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1)); // simulate actual work
std::cout << "job #" << i << " finished" << std::endl;
});
}
tp.Pause();
// Hold main thread to keep thread pool running
std::cin.get();
return0;
}
In my case, all CPUs went hyper active:
You can notice the main thread is [S] asleep due the std::cin.get(); where the 8 other threads are yielding and causing high cpu usage.
To make sure it's not an issue with htop utility, i tried with gnome system monitor which reported the same issue.
Possible Fix
Instead of yielding all threads on pause which has a bad reputation with CPU usage across multiple platforms, might as well use an std::condition_variable to wait when status is PAUSE, and to be notified when status is changed:
So instead of
while (status == PAUSE) {
std::this_thread::yield();
}
Description
When Pausing cpr::ThreadPool, all the created threads become hyper active because of yielding, the expected behavior is the created threads to use little to none of the CPU.
Example/How to Reproduce
cpr::ThreadPool
tp.Pause(); // Hold main thread to keep thread pool running std::cin.get();
Complete example code:
In my case, all CPUs went hyper active:


You can notice the main thread is [S] asleep due the
std::cin.get();
where the 8 other threads are yielding and causing high cpu usage.To make sure it's not an issue with
htop
utility, i tried with gnome system monitor which reported the same issue.Possible Fix
Instead of yielding all threads on pause which has a bad reputation with CPU usage across multiple platforms, might as well use an std::condition_variable to wait when status is PAUSE, and to be notified when status is changed:
So instead of
It can be:
and every time
status
is updated, notify thestatus_cond
which will wake up the thread to resume or stop..I have tried it locally and it seems to work fine, if there is no alternative solution I will be glad to open a PR.
Where did you get it from?
GitHub (branch e.g. master)
Additional Context/Your Environment
The text was updated successfully, but these errors were encountered: