diff --git a/ChangeLog b/ChangeLog index e0df3dc..aaa2c52 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2023-02-03 Richard Frith-Macdonald + + * GSIOThreadPool.m: If the maximum number of threads desired is + decreased so that we have more threads than we want, the excess + threads are now excluded from the set which may be acquired. + This should increase the chance of those threads being unacquired + so they can be terminated and removed from the pool. + 2023-01-25 Wolfgang Lux * GSCache.m: Raise exception when attempting to insert object with diff --git a/GSIOThreadPool.m b/GSIOThreadPool.m index d0a09bd..93c3f98 100644 --- a/GSIOThreadPool.m +++ b/GSIOThreadPool.m @@ -190,14 +190,21 @@ static NSRecursiveLock *classLock = nil; static GSIOThreadPool *shared = nil; /* Return the thread with the lowest usage. + * If there are more threads in the array than we want to use, + * those excess threads are excluded from the check so that + * their usage can drop to zero and they can be terminated. */ static GSIOThread * -best(NSMutableArray *a) +best(NSMutableArray *a, NSUInteger max) { NSUInteger c = [a count]; NSUInteger l = NSNotFound; GSIOThread *t = nil; + if (c > max) + { + c = max; + } while (c-- > 0) { GSIOThread *o = [a objectAtIndex: c]; @@ -254,7 +261,7 @@ best(NSMutableArray *a) } [classLock lock]; - t = best(threads); + t = best(threads, maxThreads); if (nil == t || ((c = [t _count]) > 0 && [threads count] < maxThreads)) { NSString *n;