Skip to content

Instantly share code, notes, and snippets.

Avatar

Nathan Allen-Wagner nathanaw

View GitHub Profile
@nathanaw
nathanaw / Parallel.Async.SemephoreSlim.cs
Last active Jul 27, 2018
Async Governor - SemphoreSlim
View Parallel.Async.SemephoreSlim.cs
// using System.Threading.Tasks
const int MAX_DEGREE_OF_PARALLELISM = 2;
static SemaphoreSlim _semaphore = new SemaphoreSlim(MAX_DEGREE_OF_PARALLELISM);
async void Main()
{
var tasks = Enumerable.Range(0, 10)
.Select(i => ExecTask(i))
.ToArray();
@nathanaw
nathanaw / Parallel.Async.DaC.cs
Created Jul 27, 2018
Async Governor - Divide and Conquer
View Parallel.Async.DaC.cs
// using System.Threading.Tasks
var maxIterations = 10000;
var maxDOP = 10;
// Divide into groups.
var parallelGroups = Enumerable.Range(0, maxIterations)
.GroupBy(r => (r % maxDOP));
var parallelTasks = parallelGroups.Select(groups =>
@nathanaw
nathanaw / Parallel.For.Async.cs
Last active Jul 27, 2018
Parallel.For() Not Async Friendly
View Parallel.For.Async.cs
using System.Threading.Tasks
void Main()
{
var options = new ParallelOptions() {
MaxDegreeOfParallelism = 2
};
Parallel.For(0, 10, options, i => ExecTaskSync(i));
You can’t perform that action at this time.