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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:18:18+00:00 2026-05-18T12:18:18+00:00

I have some code which must be thread safe whose behavior resembles this: protected

  • 0

I have some code which must be thread safe whose behavior resembles this:


protected long m_RunningValue
protected long m_RunningCounter
protected object m_Lock = new object();

public long RunningValue { get { return Interlocked.Read(m_RunningValue); } }
public long RunningCounter { get { return Interlocked.Read(m_RunningCounter); } }

public void DoCalculation(int newValue, int newQuantity)
{
   lock(m_Lock)
   {
       Interlocked.Add(ref m_RunningValueA, newValue);
       Interlocked.Add(ref m_RunningCounter, newQuantity);
       if(Interlocked.Read(ref newQuantity) == 0)
       { 
         ...m_RunningValue gets further modified here
       }
   }
}

The Calculation must lock both the value and the counter or a race condition could effect the if(…) block, however they do not need to be synchronized at all when being read out, i.e. if the counter and value changes between attempts to read both, that’s 100% ok with me.

The Interlock on the read is there for thread-safety reading of a 64-bit value.

  1. Is mixing interlocks and locks like this safe? I have read on other web pages that mixing them is unsafe, however I can’t find clarification if this means that mixing them is a great way to introduce subtle bugs, or if at a system level this can corrupt the data structures involved.

  2. Is the cost of all this interlocking (64-bit .NET 4.0 runtime) completely defeating the purpose of saving a ReaderWriterSlim lock around the property get() methods?

  • 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-05-18T12:18:19+00:00Added an answer on May 18, 2026 at 12:18 pm

    With regards to the edited version, this is not guaranteed to be thread-safe.

    Consider a 32-bit platform. This means that 64-bit long values must be accessed in two separate operations.

    It’s quite possible for one thread to read the first half of the number, then be swapped off the CPU and the second half of the value changed behind its back. Now that thread has a completely invalid value.

    The docs for Interlocked.Read make explicit mention that:

    The Read method and the 64-bit overloads of the Increment, Decrement, and Add methods are truly atomic only on systems where a System.IntPtr is 64 bits long. On other systems, these methods are atomic with respect to each other, but not with respect to other means of accessing the data. Thus, to be thread safe on 32-bit systems, any access to a 64-bit value must be made through the members of the Interlocked class.

    (emphasis mine)

    EDIT: Now that it’s been edited back this looks a little silly, but I’ll leave it here to make the point that if you’re interlocking in some places but not others, it’s kind of pointless.

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

Sidebar

Related Questions

I have some code which collects points (consed integers) from a loop which looks
I have some code which utilizes parameterized queries to prevent against injection, but I
I have some code which I am making available via RMI. If my program
I have some code which needs to ensure some data is in a mysql
I have some code which returns InnerXML for a XMLNode. The node can contain
I have some code which ignores a specific exception. try { foreach (FileInfo fi
I have some code which I did not originally create that uses _beginthreadex and
I have have some code which adds new cells to a table and fills
On a small embedded system project we have some code which we would like
I have some Ruby code which takes dates on the command line in the

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.