Class and Multi-thread safety

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nuno Magalhaes

    Class and Multi-thread safety

    Hello,

    Is there any keyword applicable to a class to make it thread-safe?
    Without having to put lock(this){} in all functions?

    Thanks.

  • Jon Skeet [C# MVP]

    #2
    Re: Class and Multi-thread safety

    Nuno Magalhaes <nunommagalhaes @hotmail.comwro te:
    Is there any keyword applicable to a class to make it thread-safe?
    Without having to put lock(this){} in all functions?
    Putting lock(this) doesn't make it thread-safe.

    Thread safety is not a simple matter - you need to carefully consider
    threading and various issues when making a class thread-safe. In
    particular, think about any code in other classes you call while
    holding a lock - if you're not careful, you can easily deadlock.

    I'd also recommend against locking on "this" to start with.

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Peter Duniho

      #3
      Re: Class and Multi-thread safety

      Nuno Magalhaes wrote:
      Is there any keyword applicable to a class to make it thread-safe?
      Without having to put lock(this){} in all functions?
      No.

      Comment

      • Nuno Magalhaes

        #4
        Re: Class and Multi-thread safety

        On 16 Out, 19:25, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
        Nuno Magalhaes <nunommagalh... @hotmail.comwro te:
        Is there any keyword applicable to a class to make it thread-safe?
        Without having to put lock(this){} in all functions?
        >
        Putting lock(this) doesn't make it thread-safe.
        >
        Thread safety is not a simple matter - you need to carefully consider
        threading and various issues when making a class thread-safe. In
        particular, think about any code in other classes you call while
        holding a lock - if you're not careful, you can easily deadlock.
        >
        I'd also recommend against locking on "this" to start with.
        >
        --
        Jon Skeet - <sk...@pobox.co m>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too
        What is the difference between locking on "this" and locking on an
        object inside the class? My object is never changed. What can go wrong?

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Class and Multi-thread safety

          Nuno Magalhaes <nunommagalhaes @hotmail.comwro te:
          What is the difference between locking on "this" and locking on an
          object inside the class? My object is never changed. What can go wrong?
          Someone else can acquire a lock on it, inadvertently blocking things at
          the wrong time. The more control you can exercise over what lock is
          taken out when, the fewer deadlock risks you'll have.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          • Mads Bondo Dydensborg

            #6
            Re: Class and Multi-thread safety

            Nuno Magalhaes wrote:
            >
            What is the difference between locking on "this" and locking on an
            object inside the class? My object is never changed. What can go wrong?
            lock(this) is not considered good practice (anymore). Check MS docs for the
            lock statement for more information.

            Regards,

            Mads

            --
            Med venlig hilsen/Regards

            Systemudvikler/Systemsdevelope r cand.scient.dat , Ph.d., Mads Bondo
            Dydensborg
            Dansk BiblioteksCente r A/S, Tempovej 7-11, 2750 Ballerup, Tlf. +45 44 86 77
            34

            Comment

            Working...