I have a static class with a few methods that just take in a byte array, parses it, and returns a structure. I need to call these methods from many separate threads. Do I need a lock() or some kind of thread-safety within the methods? I can't get my head around it.
views:
86answers:
3
+3
A:
If your method is reentrant you don't need any locks.
In general, you need locks whenever multiple threads access a shared resource. When the method just calculates something from its arguments without accessing any shared resource, there is nothing to lock.
dtb
2010-06-14 19:15:00
by your definition, his example would *not* be re-entrant.
John Weldon
2010-06-14 19:16:00
That's why I put an "If" in the beginning of my answer. From the description if his method I thought it was re-entrant, but I can't tell for sure without looking at the code.
dtb
2010-06-14 19:17:39
@John Why do you think so?
Yauheni Sivukha
2010-06-14 19:18:09
Thanks, that's what I needed to know.
JimDaniel
2010-06-14 19:30:47
A:
Yes, the lock
prevents multiple threads accessing the same data at the same time, which would lead to inconsistent / unpredictable behaviour usually.
John Weldon
2010-06-14 19:15:11
A:
If your methods have shared resource then you need synchronize access to it. It your case there is no shared resource and therefore no need to lock anything.
Yauheni Sivukha
2010-06-14 19:16:56