static class · System.Threading
ThreadPool
public static class ThreadPool
Threads that run queued work: as many as processors, started with the first work item, kept for the program.
- Tipo
- static class
- Espacio de nombres
- System.Threading
- Ensamblado
- System.Core
- Acceso
- public, static, abstract
- Archivo fuente
Threading.ant
Herencia
- Clases base
-
AnyThreadPool
Propiedades
Métodos
QueueUserWorkItem(func item)
#
Runs item on a pool thread (an error it throws ends the program).
Miembros privados e internos (8)
Miembros heredados (5)
GetTypemétodo de AnyUndescribedmétodo de AnyToStringmétodo de AnyHashmétodo de Anydestroymétodo de Any
Código fuente
ThreadPool en Antril, descompilado a partir de su Larva HIR.
// System.Threading.ThreadPool — System.Core (decompiled from Larva HIR)
import System;
into namespace System.Threading
{
/// Threads that run queued work: as many as processors, started with the first work item, kept for the program.
public static class ThreadPool
{
Mutex mutex;
Condition waiting;
Queue<func> work;
i32 workers;
i32 idle;
AtomicI32 spinning;
/// What each worker does: takes work, runs it, again. Before it sleeps it waits a little, giving the processor
/// up in turns: work often comes right after.
Work()
{
loop (true)
{
if (spinning.Increment() <= 2)
{
loop (i32 spin = 0; spin < 2000 && work.Count == 0; spin++)
Thread.SpinWait(spin < 1000 ? 20 : 0);
loop (i32 spin = 0; spin < 32 && work.Count == 0; spin++)
Thread.Yield();
}
spinning.Decrement();
mutex.Lock();
idle++;
loop (work.Count == 0)
waiting.Wait(mutex);
idle--;
func item = work.Dequeue();
mutex.Unlock();
item();
}
}
[NoInline]
Begin()
{
workers = Environment.ProcessorCount;
loop (i32 i = 0..workers)
new Thread(() => { Work(); }).Start();
}
public:
/// Runs `item` on a pool thread (an error it throws ends the program).
QueueUserWorkItem(func item)
{
mutex.Lock();
if (workers == 0)
Begin();
work.Enqueue(item);
bool someone = idle > 0;
mutex.Unlock();
if (someone)
waiting.Signal();
}
/// How many threads run the work.
i32 ThreadCount
{
get => workers;
}
}
}