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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:52:59+00:00 2026-05-28T22:52:59+00:00

I have property definition in class where i have only Counters, this must be

  • 0

I have property definition in class where i have only Counters, this must be thread-safe and this isn’t because get and set is not in same lock, How to do that?

    private int _DoneCounter;
    public int DoneCounter
    {
        get
        {
            return _DoneCounter;
        }
        set
        {
            lock (sync)
            {
                _DoneCounter = value;
            }
        }
    }
  • 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-28T22:53:00+00:00Added an answer on May 28, 2026 at 10:53 pm

    If you’re looking to implement the property in such a way that DoneCounter = DoneCounter + 1 is guaranteed not to be subject to race conditions, it can’t be done in the property’s implementation. That operation is not atomic, it actually three distinct steps:

    1. Retrieve the value of DoneCounter.
    2. Add 1
    3. Store the result in DoneCounter.

    You have to guard against the possibility that a context switch could happen in between any of those steps. Locking inside the getter or setter won’t help, because that lock’s scope exists entirely within one of the steps (either 1 or 3). If you want to make sure all three steps happen together without being interrupted, then your synchronization has to cover all three steps. Which means it has to happen in a context that contains all three of them. That’s probably going to end up being code that does not belong to whatever class contains the DoneCounter property.

    It is the responsibility of the person using your object to take care of thread safety. In general, no class that has read/write fields or properties can be made “thread-safe” in this manner. However, if you can change the class’s interface so that setters aren’t necessary, then it is possible to make it more thread-safe. For example, if you know that DoneCounter only increments and decrements, then you could re-implement it like so:

    private int _doneCounter;
    public int DoneCounter { get { return _doneCounter; } }
    public int IncrementDoneCounter() { return Interlocked.Increment(ref _doneCounter); }
    public int DecrementDoneCounter() { return Interlocked.Decrement(ref _doneCounter); }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hi i have property named. public bool ShowLabels{get; set;} and one toolbar menu button
Suppose you have class B with lazily loaded property c . And that this
I have a property called IsSecureConnection that is part of my object's interface. This
I have a property of type uint on my entity. Something like: public class
I have a property within a class, that I need to iterate through all
I have the set focus for a property which I am trying to use
I have already read Entity Framework One-To-One Mapping Issues and this is not duplicate
I have this Class which is accepting a Dictionary<string,object> which is been invoked from
I have simple static lib in xcode with the only class test.h: @interface TestClass
I have this definition in a WinForm: private BindingList<String> rollbackLog = new BindingList<String>(); I

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.