Class and Multi-thread safety - CSharp
This is a discussion on Class and Multi-thread safety - CSharp ; Hello,
Is there any keyword applicable to a class to make it thread-safe?
Without having to put lock(this){} in all functions?
Thanks....
-
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.
-
Re: Class and Multi-thread safety
Nuno Magalhaes <nunommagalhaes@hotmail.com> wrote:
> 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.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
-
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.
-
Re: Class and Multi-thread safety
On 16 Out, 19:25, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:
> Nuno Magalhaes <nunommagalh...@hotmail.com> wrote:
> > 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.com>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?
-
Re: Class and Multi-thread safety
Nuno Magalhaes <nunommagalhaes@hotmail.com> 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?
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.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
-
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/Systemsdeveloper cand.scient.dat, Ph.d., Mads Bondo
Dydensborg
Dansk BiblioteksCenter A/S, Tempovej 7-11, 2750 Ballerup, Tlf. +45 44 86 77
34
Similar Threads
-
By Application Development in forum CSharp
Replies: 2
Last Post: 10-17-2007, 09:52 AM
-
By Application Development in forum DOTNET
Replies: 2
Last Post: 08-12-2007, 08:51 PM
-
By Application Development in forum c++
Replies: 13
Last Post: 06-08-2007, 02:34 AM
-
By Application Development in forum DOTNET
Replies: 1
Last Post: 10-12-2004, 10:15 AM
-
By Application Development in forum DOTNET
Replies: 2
Last Post: 08-13-2004, 02:09 AM