0

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

jspinella
  • 2,013
  • 3
  • 25
  • 39
  • 1
    You could store the `Thread` objects and poll their `IsAlive` property. – itsme86 May 24 '19 at 22:05
  • 2
    "It waits for a thread to finish before spinning up a new one" - this is indeed false, but it does not do *exactly* what you want either... If you must have all threads started at the same time (and not let runtime manage optimal number of threads) you should use code of your second example (or similar)… (Also as personal preference I'd go for Task as syntax is easier) – Alexei Levenkov May 24 '19 at 22:06

0 Answers0