I'm building a database thread pool, the pool has a property for thread count. By default this is 0 so there's no threads until this is set. The property reads the list count directly, so I don't store anything like "FThreadCount: Integer". So, the property setter needs to be responsible for adding/removing threads as necessary.
The adding threads part is easy, but removing them becomes tricky because I cannot kill a thread which is busy. So, when this needs to remove threads, it goes into a loop with a timeout and checks each thread's busy status. For each thread it finds not busy, it removes it, repeatedly until the count is down to the desired value.
My problem is I'm afraid to even try this, and before I try to run it (which I'm far from being able to run anyway), is this a good plan?
JD Solutions
The adding threads part is easy, but removing them becomes tricky because I cannot kill a thread which is busy. So, when this needs to remove threads, it goes into a loop with a timeout and checks each thread's busy status. For each thread it finds not busy, it removes it, repeatedly until the count is down to the desired value.
My problem is I'm afraid to even try this, and before I try to run it (which I'm far from being able to run anyway), is this a good plan?
Code:
procedure TDBThreadPool.SetThreadCount(const Value: Integer);
const
WAIT_TIMEOUT = 10; //Seconds to wait for available threads
var
L: TList; //Locked thread list
T: TDBThread; //Current thread
D: Integer; //Difference in count
X, Y: Integer; //Iterators
begin
L:= LockThreads; //Acquire a locked list of threads
try
if (Value > L.Count) then begin
//Add new threads
D:= Value - L.Count;
for X := 1 to D do begin
T:= TDBThread.Create(Self);
L.Add(T);
end;
end else
if (Value < L.Count) then begin
//Remove threads
//Wait for their current processes, if any
D:= L.Count - Value;
for X := 1 to WAIT_TIMEOUT * 10 do begin
for Y := L.Count - 1 downto 0 do begin
T:= TDBThread(L[Y]);
if not T.Busy then begin //"Busy" is true when thread is processing something
T.Free;
end;
if L.Count = Value then Break;
end;
if L.Count = Value then Break;
Sleep(100); (Sleep for 1/10 of a second
end;
end;
finally
UnlockThreads;
end;
end;
JD Solutions