Home > Net >  How do I write a C# Parallel.ForEach loop where during one part of the code, only one thread can be
How do I write a C# Parallel.ForEach loop where during one part of the code, only one thread can be

Time:01-05

Not sure if this is possible without a bunch of overhead, but figured I'd ask.

Here is the current process:

Parallel loop over locations
    Sequential Loop over SQL load operations for each location

This is working just fine, except one procedure tends to deadlock if more than 1 thread is executing at the same time.

Is it possible to ensure that once the deadlocking procedure is running, all other threads wait until it is complete if they are trying to execute the same proc?

I have a feeling locking is involved, but I don't do enough of this to know for sure.

CodePudding user response:

The lock command worked for my needs.

Here is a stripped down version of how I solved it:

// This is a static variable in my main Program class
private static readonly object processLock = new object();

// This is within my processing method
Parallel.ForEach(Stores, new ParallelOptions() { MaxDegreeOfParallelism = maxdop }, store =>
{
    foreach (var sequenceFeed in Sequences.OrderBy(x => x.SequenceOrder))
    {                    
        if (sequenceFeed.Identifier == "UPC") // UPC deadlocks due to pricing code, so lock for single threaded execution
        {
            lock (processLock)
            {
                feed.Process();
            }
        }
        else
            feed.Process();
    }              
});

Thanks to @Scott Hannen in the comments for the direction.

CodePudding user response:

 Parallel loop over locations
   Sequential Loop over SQL load operations for each location

The only way that I have found to dynamically vary the number of threads running in parallel was to use tasks and SemaphoreSlim to gradually increase the number of threads running. This however is not what you're looking for. However, from your description of the process that you want to run in parallel you indicate that you are loading data from SQL. This suggests that the question you should be asking is why is your query deadlocking. If you are using SQL Server then I suggest you checkout this link, Locking and Blocking in SQL Server by Brent Ozar. I found this and other resources from his web site very helpful when I was having deadlock issues.

  •  Tags:  
  • Related