Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8186201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:06:48+00:00 2026-06-07T02:06:48+00:00

Is there a pattern to combine parallel with a thread safe calculation on the

  • 0

Is there a pattern to combine parallel with a thread safe calculation on the parallel?

Need to calculate a result in which the first step would benefit from parallel and the second is a serial process on the results of the parallel.

One option is to run the parallel and save the output to a collection and then serially process the collection and I have that working. The problem there is memory management as the collection can be very large.

Below is the serial version. Basially I want to parallel the TableQueryGetRowKeys and use that result in a thread safe manner. Tried to just Parallel the for and put a lock around the final results but rowKeys could be off. Tried aggregate but I could not figure out how to pass a collection to the aggregate let alone perform thread safe Intersect in the aggregate.

IEnumerable<string> finalResults = null;
if (partitionKey.Length == 0) return finalResults;
object lockObject = new object();
finalResults = TableQueryGetRowKeys(partitionKey[0], 0);
HashSet<string> rowKeys;
for(int i = 1; i < partitionKey.Length; i++)
{
    // IO operation to Azure Table Storage against the PartitionKey
    // so very amenable to parallel
    rowKeys = TableQueryGetRowKeys(partitionKey[i]);
    // a memory and CPU operation 
    // this should be much faster than TableQueryGetRowKeys
    // going parallel and wrapping this in a lock did not properly synch rowKeys
    finalResults = finalResults.Intersect(rowKeys); 
}
return finalResults;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:06:50+00:00Added an answer on June 7, 2026 at 2:06 am

    Assuming that TableQueryGetRowKeys is thread safe:

    var final = partitionKey.AsParallel()
                            // By returning AsParallel we can get parallel intersect
                            .Select(k => TableQueryGetRowKeys(k).AsParallel())
                            .Aggregate((x, y) => x.Intersect(y));
    
    // Using fake-ish data I see about a 30% speed-up on a 4-core machine:
    // static HashSet<string> TableQueryGetRowKeys(string prefix)
    // {
    //     // Simulate 1s of IO round-trip
    //     if (useSleep) Thread.Sleep(1000);
    //
    //     return new HashSet<string>(
    //         Enumerable.Range(0, 500)
    //                   .Select(_ => random.Value.Next(0, 500).ToString()));
    // }
    

    In stepwise fashion this algorithm works like so:

    1. partitionKey.AsParallel() turns the regular IEnumerable<string> into a ParallelQuery<string> which allows parallel processing of the sequence.
    2. Next, ParallelEnumerable.Select is used to call TableQueryGetRowKeys in parallel.
    3. The result of each call to TableQueryGetRowKeys is then wrapped in a ParallelQuery<T> using AsParallel().
    4. ParallelEnumerable.Intersect is used as an aggregation function over each “parallel-enabled” enumeration returned by TableQueryGetRowKeys.

    In effect, this could be used in serial to replace your previous code by removing the AsParallel calls, like so:

    var serialEquivalent = partitionKey.Select(k => TableQueryGetRowKeys(k))
                                       .Aggregate((x,y) => x.Intersect(y));
    

    You can “convince” yourself that this is equivalent to your method when you look at the meat and potatoes of your implementation:

    IEnumerable<string> results = SomeMethod(0);
    for (int ii = 1; ii < count; ++ii)
    {
        results = results.Intersect(SomeMethod(ii));
    }
    

    Rewriting the above using + instead of Intersect:

    int results = SomeMethod(0);
    for (int ii = 1; ii < count; ++ii)
    {
        results = results + SomeMethod(ii);
    }
    

    Now it becomes clear that Intersect could be used in place of other more “common” aggregation functions (e.g. mathematical operators).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Isn't there a Design Pattern who describes how to high cohesion? I need some
I need to authenticate my iPhone app on the website. Is there a pattern
Observer pattern: There are 2 variants of it. Where Subjects informs all the observers
Is there a pattern that I could apply to refactor this code? The only
Is there any pattern how to deal with a lot of object instantiations (40k
Is there a pattern, Xml structure, architecture technique we can use to create simple
Is there a pattern that can handle recurring dimension in data warehouse? I've got
Is there an established pattern for implementing undo/redo functionality in clojure or in fp
How to find the below pattern is there or not in a given string
When I was working with XmlDOM in Asp.Net, there was a pattern like this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.