1 using System.Diagnostics.CodeAnalysis;
3 namespace System.Threading
6 /// A helper class to get the number of processors, it updates the numbers of processors every sampling interval.
8 internal static class PlatformHelper
10 private const int PROCESSOR_COUNT_REFRESH_INTERVAL_MS = 30000; // How often to refresh the count, in milliseconds.
11 private static volatile int s_processorCount; // The last count seen.
12 private static volatile int s_lastProcessorCountRefreshTicks; // The last time we refreshed.
15 /// Gets the number of available processors
17 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "Reviewed for thread safety")]
18 internal static int ProcessorCount
22 int now = Environment.TickCount;
23 int procCount = s_processorCount;
24 if (procCount == 0 || (now - s_lastProcessorCountRefreshTicks) >= PROCESSOR_COUNT_REFRESH_INTERVAL_MS)
26 s_processorCount = procCount = Environment.ProcessorCount;
27 s_lastProcessorCountRefreshTicks = now;
35 /// Gets whether the current machine has only a single processor.
37 internal static bool IsSingleProcessor
39 get { return ProcessorCount == 1; }