The best way to do it, I think, is to maintain two variables in a central, semaphore-protected table: the limit to the number of threads that you want, and the count of threads that you now have. If this number is increased, then the head-honcho thread simply adds more children. If the number is decreased, the head-honcho signals all the sleeping children to wake up.
Each child (entering the central table one-at-a-time due to the semaphore) notices if the limit is now exceeded. If so, the thread decrements the count and dies. Otherwise, it looks for work-to-do as usual.
In this way, the system will adjust itself in a matter of a few milliseconds to any reduction in the limit. But, no thread is killed: each thread is, as always, an obedient custodian of its own life-span. And, no thread fails to complete whatever unit of work it was in the process of doing at the time that the limit was lowered.
|