Skip to content

Instantly share code, notes, and snippets.

@gfoidl
Last active December 30, 2017 22:03
Show Gist options
  • Save gfoidl/83e0bbda2d9e9e98bcce38d0b7e5091e to your computer and use it in GitHub Desktop.
Save gfoidl/83e0bbda2d9e9e98bcce38d0b7e5091e to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp1
{
static class Program
{
static void Main()
{
var ranges = new ConcurrentDictionary<int, List<(int Start, int End)>>();
const int N = 10_000;
Parallel.ForEach(
new MyPartitioner(N),
range =>
{
AddRange(range);
for (int i = range.Start; i < range.End; ++i)
for (int j = 0; j < 1_000 * (N - range.Item2); ++j)
;
}
);
foreach (var item in ranges.OrderBy(i => i.Key))
{
Console.WriteLine($"Thread-ID: {item.Key}");
foreach (var range in item.Value.OrderBy(r => r.Start))
Console.WriteLine($"\t{range,-15}\t{range.End - range.Start}");
}
//-----------------------------------------------------------------
void AddRange((int Start, int End) range)
{
var list = new List<(int Start, int End)> { range };
ranges.AddOrUpdate(
Thread.CurrentThread.ManagedThreadId,
list,
(id, l) =>
{
l.Add(range);
return l;
}
);
}
}
}
}
@gfoidl
Copy link
Author

gfoidl commented Dec 30, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment