It's better than the previous version, but I suspect it would still slow things down noticably. However, I think some measurements would be in order to know for sure. (On various platforms of course)
/ Fredrik (Naranek) Hubinette (Real Build Master)
Previous text:
2002-09-06 16:59: Subject: Frequent context switches
This seems to be some sort of decaying average algorithm. Wouldn't it be necessary that the number of calls to check_threads per second varies in a somewhat continuous way for it to work? I.e. isn't it possible that the program shifts the working set from one that causes 100 k calls/sec to one that only causes 1 k calls/sec? Then this could produce time slices of several seconds before it readjusts. Sure, it's not important that the time slices are exactly uniform, but that would probably be too erratic.
What about this crude but obvious solution:
static int div_; if(div_++ & 255)
return; if (clock() - thread_start_clock < (clock_t) (CLOCKS_PER_SEC / 20)) return;
It would only put a cap on very short time slices. (Ideally one would like to put a cap on long ones too, but I guess there's no real risk that the function gets called as seldom as about 100 times/sec.)
/ Martin Stjernholm, Roxen IS