In a long running Windows Service I have a custom thread pool manager that has a loop which continuously looks through a couple lists for idle threads.
Given multiple uses of the same predicate, is there any benefit to saving off the lambda as a Func?
How might the recommendation change based on the count of threads this code is interacting with, or when the Func might need to be passed to other methods to help with the work?
while (_isRunning )
{
...sleep a bit to prevent thrashing
while ( _threads.Any(t => t.IsIdle) || _autreThreads.Any(t => t.IsIdle)
{
var assignee = _threads.FirstOrDefault(t => t.IsIdle) ?? _autreThreads.FirstOrDefault(t => t.IsIdle);
...give 'em something to do
}
}
In place of each of the lambdas I could use the following, defined once outside for the loop, for reuse.
Func<WorkerThread, bool> isIdleCondition = t => t.IsIdle;