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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:25:48+00:00 2026-05-24T08:25:48+00:00

Is there any reason why you would create locks around the getter and setter

  • 0

Is there any reason why you would create locks around the getter and setter of a boolean property like this?

  private _lockObject = new object();
  private bool _myFlag;
  public bool MyFlag
  {
    get
    {
      lock (_lockObject)
      {
        return _myFlag;
      }
    }
    set
    {
      lock (_lockObject)
      {
        _myFlag = 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-24T08:25:49+00:00Added an answer on May 24, 2026 at 8:25 am

    Well, you don’t need locks necessarily – but if you want one thread to definitely read the value that another thread has written, you either need locks or a volatile variable.

    I’ve personally given up trying to understand the precise meaning of volatile. I try to avoid writing my own lock-free code, instead relying on experts who really understand the memory model.

    EDIT: As an example of the kind of problem this can cause, consider this code:

    using System;
    using System.Threading;
    
    public class Test
    {
        private static bool stop = false;
    
        private bool Stop
        {
            get { return stop; }
            set { stop = value; }
        }
    
        private static void Main()
        {
            Thread t = new Thread(DoWork);
            t.Start();
            Thread.Sleep(1000); // Let it get started
            Console.WriteLine("Setting stop flag");
            Stop = true;
            Console.WriteLine("Set");
            t.Join();
        }
    
        private static void DoWork()
        {
            Console.WriteLine("Tight looping...");
            while (!Stop)
            {
            }
            Console.WriteLine("Done.");
        }
    }
    

    That program may or may not terminate. I’ve seen both happen. There’s no guarantee that the “reading” thread will actually read from main memory – it can put the initial value of stop into a register and just keep using that forever. I’ve seen that happen, in reality. It doesn’t happen on my current machines, but it may do on my next.

    Putting locks within the property getter/setter as per the code in the question would make this code correct and its behaviour predictable.

    For more on this, see this blog post by Eric Lippert.

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

Sidebar

Related Questions

Is there any reason something like this would not work? This is the logic
Is there any reason this function call would not return 'result'? CREATE OR REPLACE
Are there any good reasons for why you would include JavaScript like this: <script
Is there any reason why JQuery would not work when used in an ASPX
Question: Is there any reason Autocomplete=off on a ASP:Textbox would not be working in
I'm trying to do this, and failing. Is there any reason why it wouldn't
Considering there are so many draconian firewalls in the world, is there any reason
Is there any reason not to use the bitwise operators &, |, and ^
Is there any reason why I should pick JSON over XML, or vice-versa if
Is there any reason to use a varchar field instead of a date field

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.