reader lock on get-only property

michael.proepster's Avatar

michael.proepster

20 Sep, 2017 11:41 AM

Hi,

why do you need to acquire a reader lock for a get-only property?

[ReaderWriterSynchronized]
public class MyClass
{
     public bool SomeProperty { get; }
}

It is read only. I can't think of any issue If you wouldn't acquire a lock. This has massive performance benefits.
I am about to put [ExplicitlySynchronized] over all those properties in my code base, but that is annoying. Am I missing something?

Regards
Michael

  1. 1 Posted by michael.proepst... on 20 Sep, 2017 12:00 PM

    michael.proepster's Avatar

    I tried a slightly different example:

     [ReaderWriterSynchronized]
     internal class MainModel
     {
        public ImmutableType ImmutableChild
        {
            get { return _immutableChild; }
        }
    
        [Child]
        private readonly ImmutableType _immutableChild;
    }
    
    
    [Immutable]
    internal class ImmutableType
    {
        public string SomeProperty { get; set; }
    }
    

    Now you do NOT acquire the reader lock on ImmutableType.SomeProperty?

    Why is it ok to read here without lock, but not ok on a read-only property in the main class?

  2. Support Staff 2 Posted by PostSharp Techn... on 21 Sep, 2017 12:59 PM

    PostSharp Technologies's Avatar

    Hello,

    the behavior of [Immutable] is because of properties of this threading model (i.e. setter throws an exception and thus getter is thread safe). If RWS object has an Immutable child, there is no need to enforce the RWS threading model on it as [Immutable] is more strict. You can find threading model compatibilities listed here.

    The readonly property acquiring lock is merely because currently method bodies are not analyzed in this way. We may be adding that in the future, but currently the best option is to use ExplicitlySynchronized on readonly properties.

    Best regards,
    Daniel

  3. 3 Posted by michael.proepst... on 21 Sep, 2017 01:09 PM

    michael.proepster's Avatar

    Ok, I will do that.

    Regards
    Michael

  4. Support Staff 4 Posted by PostSharp Techn... on 21 Sep, 2017 02:42 PM

    PostSharp Technologies's Avatar

    Hello,

    you will likely run into #15521 mentioned in the other thread. Please use the attribute on accessors instead of child property/field.

    Best,
    Daniel

  5. Support Staff 5 Posted by PostSharp Techn... on 09 Oct, 2017 12:07 PM

    PostSharp Technologies's Avatar

    Hello,

    We're closing the ticket for now as the bug has been internally filed as issue #15521. We will contact you as soon as the bug fix has been released.

    For more details on our support policies and prioritization of bug fixes, please visit https://www.postsharp.net/support/policies

    Thanks,
    PostSharp Team

  6. PostSharp Technologies closed this discussion on 09 Oct, 2017 12:07 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac