I want to parallelize just a part of a .NET Core console app, but I also want to dynamically spin up threads from a command line argument. I'm utilizing the modulus operator to roughly-evenly split a List of items between the threads.
I have tried a few things:
for (var t = 0; t < numThreads; t++)
{
//spin up a thread
var group = data.Where(x => x.ID % (ulong)numThreads == (ulong)t).ToList();
jobThreads.Add(new JobThread
{
ThreadNumber = t,
Data = group,
});
}
Parallel.ForEach(jobThreads, (thread) =>
{
DoWork(thread.Data);
});
Parallel.ForEach is neat syntactic sugar, but it is not actually running the threads in parallel. It waits for a thread to finish before spinning up a new one.
Using the for loop to create a list of Task objects, which I then WaitAll does not work either. It also runs in series rather than parallel.
The "original" actually works best, it runs the threads independently/in parallel, but I am not able to tell when they are all done before continuing to the next part of the program (which runs as though there are no threads preceding it, as expected - I just don't know if there is a way to "await" them when they are created en masse via the for loop.
for (var t = 0; t < numThreads; t++)
{
//spin up a thread
var group = data.Where(x => x.ID % (ulong)numThreads == (ulong)t).ToList();
Thread thread = new Thread(new ParameterizedThreadStart(DoWork);
thread.Start(group);
}
Edit: Ah yep that there duplicate is exactly what I am looking for. Thanks