1

I was a bit confused on the concept of locking in some scenarios. Let me explain, suppose I have the following:

private readonly MyClass myObj;

private void Go(object state)
{
    // call an instance member of myObj, but does it need to be locked?
    myObj.SomeMethod(state);
}

So the question is does myObj need to be locked when calling SomeMethod? It is read-only, however since multiple threads can call the instance method SomeMethod of myObj with varying states will this not cause issues??

Thanks.

3 Answers 3

6

The variable is readonly, but the object may be mutable or immutable - and even that doesn't really tell you whether or not it can be used safely from multiple threads.

In other words, it depends on the implementation of MyClass, and whether you expect Go to be called from multiple threads referring to the same MyClass instance. (These days I tend to write classes not to be thread safe, but typically each thread gets its own set of objects to play with - so I'd probably have an instance of the class containing Go, and an instance of MyClass, but know that other threads shouldn't be using the same instances.)

Sign up to request clarification or add additional context in comments.

5 Comments

Isn't the concern for locking more with SomeMethod and it's subsequent resources as opposed to Go?
@Smudge202: It depends - if MyClass doesn't claim to be threadsafe but the class containing Go does, then the locking should be in Go. If MyClass.SomeMethod is meant to be thread-safe (for a suitable meaning of that term...) then Go doesn't need locking.
Fair enough. I'd find that all horribly backwards though, locking resources in the Go method on behalf of SomeMethod which isn't intended to be thread safe. Personal preference I guess. I too prefer the non-threadsafe way and ensuring apartments/independent instances for threads where possible. Much simpler.
Thank you. That has cleared it up a little. But I should of been more clearer, basically Go is the callback method in the thread pool, and myObj is a property in the class (which contains implementation of Go). Hence myObj is accessible to all threads.
I am quite sure that myObj should not change internally, but what I was having problems with is the fact that myObj.SomeMethod is being called from multiple threads, hence is it not possible that during the execution the parameter of myObj.SomeMethod (which has argument state in this case) can change? So the argument of one thread to myObj.SomeMethod can pollute the execution of myObj.SomeMethod in another thread.
4

It depends, if SomeMethod is a thread safe method then you don't need locking. Otherwise you may need to use lock.

2 Comments

I do not think it is thread safe! (It is the Match method of a regex object). Thanks.
Actually, matching methods on Regex objects are thread safe: msdn.microsoft.com/en-us/library/6h453d2h.aspx
1

It's hard to say for certain without more information on what you mean by "varying states", but in general, if a method is only reading from fields that you know won't be changed on other threads, you should be fine without a lock.

If you're only relying on readonly, though, then that's not good enough, since the object itself may be changing during calls from different threads.

1 Comment

What I ment was that myObj.SomeMethod will be called from different threads, hence the argument to myObj.SomeMethod will vary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.