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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:58:38+00:00 2026-05-20T10:58:38+00:00

Say I have a property whose setter is protected by a lock, but without

  • 0

Say I have a property whose setter is protected by a lock, but without any lock around the getter, e.g.

private long _myField;
public long MyProperty
{
    get { return _myField; }
    set { lock(whatever) _myField = value; }
}

In addition to synchronizing writes (but not reads), the lock, or rather Monitor.Exit, should cause a volatile write. Let’s now say we have two threads A and B, and the following sequence happens:

  1. A reads the current value of MyProperty.
  2. B writes a new value to MyProperty.
  3. A reads the current value of MyProperty again.

Q: Is A now guaranteed to see the new value? Or did our lock just ensure that B writes to main memory in a timely manner, but not that other threads read a fresh value? Or could the answer even depend on whether we’re running in .Net 2+ or a “weaker” ECMA implementation?

  • 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-20T10:58:39+00:00Added an answer on May 20, 2026 at 10:58 am

    No, since the read does not have the explicit memory barrier, it is not “guaranteed” to see the new value.

    You can use a ReaderWriterLockSlim to insure that a) the writes lock each other and b) the reads always pickup the new value.

    private readonly ReaderWriterLockSlim _myFieldLock = new ReaderWriterLockSlim();
    private long _myField;
    public long MyProperty
    {
        get 
        {
            _myFieldLock.EnterReadLock();
            try
            {
                return _myField;
            }
            finally
            {
                _myFieldLock.ExitReadLock();
            }
        }
        set
        {
            _myFieldLock.EnterWriteLock();
            try
            {
                _myField = value;
            }
            finally
            {
                _myFieldLock.ExitWriteLock();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say you have a button whose command property is bound to some ICommand of
Let's say I have a property which I want shown in a DataGridView, but
Say you have a class with an event property. If you instantiate this class
Say I have a class Customer which has a property FirstName . Then I
Say I have two classes and have a requirement that the primary key property
Say I have a template which displays a view based on a property: {{#if
Lets say I have a LunchBox class with a property for FreezerPack that has
let's say I have HTML stored in HTMLElement object. It's outerHTML property looks like
I have a boolean property in my ViewModel, named lets say IsNotSupported that is
say, I have a partial class that contains a custom property like this public

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.