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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:45:48+00:00 2026-05-13T12:45:48+00:00

Could you describe two methods of synchronizing multi-threaded write access performed on a class

  • 0

Could you describe two methods of synchronizing multi-threaded write access performed
on a class member?

Please could any one help me what is this meant to do and what is the right answer.

  • 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-13T12:45:49+00:00Added an answer on May 13, 2026 at 12:45 pm

    When you change data in C#, something that looks like a single operation may be compiled into several instructions. Take the following class:

    public class Number {
        private int a = 0;
        public void Add(int b) {
            a += b;
        }
    }
    

    When you build it, you get the following IL code:

    IL_0000:  nop
    IL_0001:  ldarg.0
    IL_0002:  dup
    // Pushes the value of the private variable 'a' onto the stack
    IL_0003:  ldfld      int32 Simple.Number::a
    // Pushes the value of the argument 'b' onto the stack
    IL_0008:  ldarg.1
    // Adds the top two values of the stack together
    IL_0009:  add
    // Sets 'a' to the value on top of the stack
    IL_000a:  stfld      int32 Simple.Number::a
    IL_000f:  ret
    

    Now, say you have a Number object and two threads call its Add method like this:

    number.Add(2); // Thread 1
    number.Add(3); // Thread 2
    

    If you want the result to be 5 (0 + 2 + 3), there’s a problem. You don’t know when these threads will execute their instructions. Both threads could execute IL_0003 (pushing zero onto the stack) before either executes IL_000a (actually changing the member variable) and you get this:

    a = 0 + 2; // Thread 1
    a = 0 + 3; // Thread 2
    

    The last thread to finish ‘wins’ and at the end of the process, a is 2 or 3 instead of 5.

    So you have to make sure that one complete set of instructions finishes before the other set. To do that, you can:

    1) Lock access to the class member while it’s being written, using one of the many .NET synchronization primitives (like lock, Mutex, ReaderWriterLockSlim, etc.) so that only one thread can work on it at a time.

    2) Push write operations into a queue and process that queue with a single thread. As Thorarin points out, you still have to synchronize access to the queue if it isn’t thread-safe, but it’s worth it for complex write operations.

    There are other techniques. Some (like Interlocked) are limited to particular data types, and there are even more (like the ones discussed in Non-blocking synchronization and Part 4 of Joseph Albahari’s Threading in C#), though they are more complex: approach them with caution.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is an additional SDK for Surface. Take a look… May 13, 2026 at 8:37 pm
  • Editorial Team
    Editorial Team added an answer Assuming that the instances are equal because the hash codes… May 13, 2026 at 8:37 pm
  • Editorial Team
    Editorial Team added an answer To control the namespace alias, use XmlSerializerNamespaces. [XmlRoot("Node", Namespace="http://flibble")] public… May 13, 2026 at 8:37 pm

Related Questions

Say you have these two methods: Number 1: void AddPerson(Person person) { // Validate
I'm a controls developer and a relative newbie to unit testing. Almost daily, I
I'm curious about the UserDetailsManager interface. It's not mentioned in the reference guide and
In my pre-ASP.NET development environment, there was a near-universal best practice: * NEVER use
First of all, I'm not sure if the title accurately describes what I'm referring

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.