This commit is contained in:
pithikos 2015-03-06 16:13:01 +00:00
commit 33e15ec369
2 changed files with 13 additions and 2 deletions

View File

@ -20,3 +20,14 @@ immedietely after you destroyied the pool, the threads will be freed
anyway by the OS. If you eitherway destroy the pool in the middle of your
program it doesn't matter again since the program will not exit immediately
and thus threads will have more than enough time to terminate.
###Why does wait() use all my CPU?
Normally `wait()` will spike CPU usage to full when called. This is normal as long as it doesn't last for more than 1 second. The reason this happens is that `wait()` goes through various phases of polling (what is called smart polling).
* Initially there is no interval between polling and hence the 100% use of your CPU.
* After that the polling interval grows exponentially.
* Finally after x seconds, if there is still work, polling falls back to a very big interval.
The reason `wait()` works in this way, is that the function is mostly used when someone wants to wait for some calculation to finish. So if the calculation is assumed to take a long time then we don't want to poll too often. Still we want to poll fast in case the calculation is a simple one. To solve these two problems, this seemingly awkward behaviour is present.

View File

@ -182,7 +182,7 @@ void thpool_wait(thpool_* thpool_p){
/* Continuous polling */
double timeout = 1.0;
time_t start, end;
double tpassed;
double tpassed = 0.0;
time (&start);
while (tpassed < timeout &&
(thpool_p->jobqueue_p->len || thpool_p->num_threads_working))
@ -232,7 +232,7 @@ void thpool_destroy(thpool_* thpool_p){
/* Give one second to kill idle threads */
double TIMEOUT = 1.0;
time_t start, end;
double tpassed;
double tpassed = 0.0;
time (&start);
while (tpassed < TIMEOUT && thpool_p->num_threads_alive){
bsem_post_all(thpool_p->jobqueue_p->has_jobs);