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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:33:50+00:00 2026-05-10T23:33:50+00:00

I was reading the following article: http://msdn.microsoft.com/en-us/magazine/cc817398.aspx Solving 11 Likely Problems In Your Multithreaded

  • 0

I was reading the following article: http://msdn.microsoft.com/en-us/magazine/cc817398.aspx ‘Solving 11 Likely Problems In Your Multithreaded Code’ by Joe Duffy

And it raised me a question: ‘We need to lock a .NET Int32 when reading it in a multithreaded code?’

I understand that if it was an Int64 in a 32-bit SO it could tear, as it is explained in the article. But for Int32 I imagined the following situation:

class Test {   private int example = 0;   private Object thisLock = new Object();    public void Add(int another)   {     lock(thisLock)     {       example += another;     }   }    public int Read()   {      return example;   } } 

I don’t see a reason to include a lock in the Read method. Do you?

Update Based on the answers (by Jon Skeet and ctacke) I understand that the code above still vulnerable to multiprocessor caching (each processor has its own cache, unsynchronized with others). All the three modifications bellow fix the problem:

  1. Adding to ‘int example’ the ‘volatile’ property
  2. Inserting a Thread.MemoryBarrier(); before the actual reading of ‘int example’
  3. Read ‘int example’ inside a ‘lock(thisLock)’

And I also think that ‘volatile’ is the most elegant solution.

  • 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. 2026-05-10T23:33:50+00:00Added an answer on May 10, 2026 at 11:33 pm

    Locking accomplishes two things:

    • It acts as a mutex, so you can make sure only one thread modifies a set of values at a time.
    • It provides memory barriers (acquire/release semantics) which ensures that memory writes made by one thread are visible in another.

    Most people understand the first point, but not the second. Suppose you used the code in the question from two different threads, with one thread calling Add repeatedly and another thread calling Read. Atomicity on its own would ensure that you only ended up reading a multiple of 8 – and if there were two threads calling Add your lock would ensure that you didn’t ‘lose’ any additions. However, it’s quite possible that your Read thread would only ever read 0, even after Add had been called several times. Without any memory barriers, the JIT could just cache the value in a register and assume it hadn’t changed between reads. The point of a memory barrier is to either make sure something is really written to main memory, or really read from main memory.

    Memory models can get pretty hairy, but if you follow the simple rule of taking out a lock every time you want to access shared data (for read or write) you’ll be okay. See the volatility/atomicity part of my threading tutorial for more details.

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

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer How's about this for a succinct description: Procedural Programming is… May 12, 2026 at 1:04 am
  • Editorial Team
    Editorial Team added an answer One constructor for shared_ptr takes the destructor method, and you… May 12, 2026 at 1:04 am
  • Editorial Team
    Editorial Team added an answer There are a lot of applications for which a cluster… May 12, 2026 at 1:04 am

Related Questions

.NET 3.5, C# I have a web app with a search feature. Some of
I've just recently started learning HTML/CSS and I've been trying to teach myself sound
I was reading the following question - How safe would it be to use
I was reading the linked question which leads me to ask this question. Consider

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.