METHOD FOR A WORKER THREAD TO START COPIES OF ITSELF TO HANDLE HALF OF ITS LOAD WHEN OVERLOADED
Original Publication Date: 2000-Apr-01
Included in the Prior Art Database: 2003-Jun-11
Described is a method to guarantee that a workload can be handled by the appropriate number of threads at any one time; that is, more threads will be started when necessary, while threads will be killed when no longer needed. In this way, only one thread is needed when the workload is low, but many extra threads can be started when the workload is high. This method is elegant, in that it does not rely on some separate "controller" thread. It also uses the same worker thread code for all threads. The specific embodiment of this method is that of a worker thread that is responsible for checking for cancellation of active jobs, for all ports monitored by a Windows NT 4.0 or Windows 2000 port monitor. The basic method entails an overloaded worker thread starting another worker thread and assigning the new thread half of the work. For example, in the above-mentioned embodiment, the thread can break the ports it is responsible for into two halves. At the start, the worker thread is responsible for port numbers 1 through 99999999 (99999999 being the special value meaning “until the last port”; using this value adds significant flexibility in that it allows ports to be added and deleted without the worker thread's having to keep a current count). When the load is split into two, the list of ports is traversed and the workload is split into one thread handling port numbers 1 through 8, for example, and the other thread handling port numbers 9 through 99999999. If a worker thread that is supposed to run every X seconds takes longer than X seconds to do the tasks assigned to it, it is defined to be overloaded. In this case, it will start another worker thread and hand half its tasks to this new thread. The process recurses, with each thread starting another worker thread if necessary. This can result in a complicated set of worker threads: 1. Worker thread 1 (WT1), handling 1 to 99999999, is overloaded; starts WT2, which is responsible for 1 to 59; WT1 now responsible for 60 to 99999999.