Skip to content

Instantly share code, notes, and snippets.

@itn3000
Created September 8, 2023 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itn3000/c616e06be881f3a823ef3d9c2111f336 to your computer and use it in GitHub Desktop.
Save itn3000/c616e06be881f3a823ef3d9c2111f336 to your computer and use it in GitHub Desktop.
thread processor count conflict test
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