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

  • Home
  • SEARCH
  • 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 9240703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:14:13+00:00 2026-06-18T08:14:13+00:00

I have this DataTable: DataTable dt = GetDatatTable(); One of its column is Amount

  • 0

I have this DataTable:

DataTable dt = GetDatatTable();

One of its column is Amount (decimal)

I want to summarize it as fast as I can using TPL.

  object obj  = new Object();
  var total=0m;
  Parallel.For (1, dt.Rows.Count+1  ,i => {lock (obj) total += Decimal.Parse(dt.Rows[i-1]["Amount"]) });

But I really dont want to lock around many times.

Question #1

Is there any other alternative which reduce the extensive locks ?

Question #2

I don’t understand why should I need to protect the total accumulator

  • Does the protection is for the += or for multi thread updating the total ?

    I mean look at the following flow , a Volatile field can solve it easily.

    let’s say total=0
    and the DataTable items are 1,2,3

    1) first thread : total= total+1. ( total=1)

    2) second thread : total = total+___stop__ ( context switch , thread 3 comes in with value 3) ___val=_3____ ( total =1+3=4)

    3) context switch back to thread 2 total=4+2 = 6.

    so everything seems to be fine .

I must be missing something here.

P.s.
I know I can do it with :

ParallelEnumerable.Range (1, dt.Rows.Count+1).Sum (i => Decimal.Parse(dt.Rows[i-1]["Amount"]) )

But I want to learn to do it with Parallel.For

  • 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-18T08:14:14+00:00Added an answer on June 18, 2026 at 8:14 am

    Yes, there are alternatives to reduce the locks:

    1. Use the overload of Parallel.For() that supports local data. This way, you need synchronization only in the localFinally delegate (but you shouldn’t forget it there).
    2. Use Interlocked.Add(). This won’t work in your case, because there are overloads only for int and long, not for decimal.
    3. Don’t use parallel processing. With a very simple operation like this one, it’s quite possible that the overhead of parallel processing will be more than the gains in speed.
    4. Use PLINQ:

      var total =
          ParallelEnumerable.Range(0, dt.Rows.Count)
                            .Select(i => Decimal.Parse(dt.Rows[i]["Amount"]))
                            .Sum();
      

    Regarding your thread-safety question, you’re assuming that after the “context switch” (I use scary quotes, because on multicore CPUs, there doesn’t have to be any context switch for this issue to occur), the thread will read the current value of total again. But in fact, it already read the old value, which is now saved in a register. So, the result in step 3 will become 1 + 2 = 3.

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

Sidebar

Related Questions

I have a on memory Datatable like this. I want to produce xml file
i have this code using (SqlConnection conn = new SqlConnection(ConnectionString)) { conn.Open(); DataTable Databases
I have a DataTable, and i want to grouped with a column . I
i have retrived this datatable from datasource and i want to add a more
I have this datatable That I want to have different row color for each
I have this dataTable: <h:dataTable value=#{orderController.orderList} var=order styleClass=table table-striped> <h:column> <f:facet name=header>#</f:facet> <h:outputText value=#{order.orderNo}
I have two functions that I want to combine using generics. static public DataTable
I have big DataTable I want to get subset of this DataTable represented as
I have this datatable, actually a list of datatable List<DataTable> lstDataSource , now for
I have an asp:ImageButton with OnClick=Btn_OnClick. In Btn_OnClick I have this line: DataTable dtTable

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.