Re: Class and Multi-thread safety

Tech-Archive recommends: Speed Up your PC by fixing your registry



Nuno Magalhaes <nunommagalhaes@xxxxxxxxxxx> 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@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • Re: Trap when using Random() ... make sure it is not being changed too quickly
    ... declared within the same time slice will have the same seed. ... be careful of the fact that Random isn't thread-safe. ... and pieces here and there, and have sort of a 10000 m view, but if you ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Class and Multi-thread safety
    ... Putting lockdoesn't make it thread-safe. ... threading and various issues when making a class thread-safe. ... holding a lock - if you're not careful, ... I'd also recommend against locking on "this" to start with. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: streams and threads... why?
    ... >> I acknowledge that in general, thread safety is a very important issue ... Neither of which require locking of the stream itself. ... > I would expect the whole java.io API to be thread-safe. ... I agree that if the API requirements state that some methods be thread-safe ...
    (comp.lang.java.programmer)
  • Re: race on multi-processor solaris
    ... Atomic as opposed to merely thread-safe? ... Detlefs seems to ... Java ptrs are atomic. ... You get valid values no matter how ...
    (comp.unix.solaris)
  • Re: GetEnvironmentVariable
    ... notion of thread safety for example .. ... I don't know if the environment block is locked ... guess SetEnvironmentVariable is thread-safe, ... you could corrupt the environment, which is a data structure Windows ...
    (microsoft.public.vc.mfc)