Closed baderouaich closed 5 months ago
@baderouaich thanks for reporting. I will try to have a look at it over the weekend!
Found the cause:
while (status == PAUSE) {
std::this_thread::yield();
}
A conditional variable based approach would be better. Working on it.
PR for it is here: https://github.com/libcpr/cpr/pull/1039
Could you please confirm this is fixed now.
There is an other bug inside the thread pool I found during debugging this: https://github.com/libcpr/cpr/issues/1040
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
Create and start a
cpr::ThreadPool
Submit some work to the thread pool so all the threads will be created
Pause the thread pool
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:
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 withhtop
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