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 8092121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:13:14+00:00 2026-06-05T20:13:14+00:00

I have a time-consuming static C# method for creating an array (of double :s)

  • 0

I have a time-consuming static C# method for creating an array (of double:s) and have therefore parallelized the operation.

Since I am creating the array before entering the loop and not tampering with its reference after that, I am thinking that it should be sufficient to lock the array itself while updating it within the parallel loop.

Is it OK to lock on the array itself or would I potentially face some performance or deadlock problems with this approach? Is it better to create a separate lock variable to put the lock on?

Here is some sample code to illustrate:

static double[] CreateArray(int mn, int n)
{
  var localLock = new object();    // Necessary?
  var array = new double[mn];

  Parallel.For(0, n, i =>
  {
    ... lengthy operation ...

    lock (array)    // or is 'lock (localLock)' required?
    {
      UpdatePartOfArray(array);
    }
  });

  return array;
}
  • 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-05T20:13:15+00:00Added an answer on June 5, 2026 at 8:13 pm

    Since the array here is a reference type, isn’t reassigned during the operations, and isn’t exposed elsewhere yet (where some other code could lock it), yes, it can suffice as the lock object itself. However, if the updates are to different parts of the array, i.e.

    array[i] = ... // i is separate
    

    then there is no need to lock anything.

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

Sidebar

Related Questions

In My application have time consuming process.There fore i try to do that operation
I have a function a time consuming operation that is done I want to
I have a time consuming loop I would like to execute in parallel. Pseudocode:
I have a time-consuming method with non-predefined number of iterations inside it that I
have one time consuming step that flattens a bunch of files. basically i'd like
I have a ASP.NET MVC3 application that handles time-consuming processes (copying a large file
I have a Maven project which performs a number of time consuming tests as
I am running a time consuming program a lot of times. I have the
We have a large internal data collection website. I don't have time to create
I need to learn RegEx but don't have time to figure this out right

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.