Created
September 8, 2023 10:18
-
-
Save itn3000/c616e06be881f3a823ef3d9c2111f336 to your computer and use it in GitHub Desktop.
thread processor count conflict test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading.Tasks; | |
using System.Threading; | |
using System.Linq; | |
using System; | |
await ThreadIdTest(100, 100); | |
async Task ThreadIdTest(int minThreads, int minCompletionNum) | |
{ | |
ThreadPool.GetMinThreads(out var currentMinThreads, out var currentCompletionNum); | |
try | |
{ | |
var ar = new long[Environment.ProcessorCount]; | |
ThreadPool.SetMinThreads(minThreads, minCompletionNum); | |
await Task.WhenAll(Enumerable.Range(0, 10000).Select(async (idx) => | |
{ | |
for (int i = 0; i < 10000; i++) | |
{ | |
await Task.Yield(); | |
ar[Thread.GetCurrentProcessorId()] += 1; | |
} | |
})); | |
foreach(var (idx, num) in ar.Select((idx, num) => (idx, num))) | |
{ | |
Console.WriteLine($"{idx}: {num}"); | |
} | |
// This cannot be 100000000 | |
Console.WriteLine($"total: {ar.Sum()}"); | |
} | |
finally | |
{ | |
ThreadPool.SetMinThreads(currentMinThreads, currentCompletionNum); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment