1

I have a function inside a threaded object, this function accepts several parameters and I don't know if when many threads try to use this function this threads will change the parameter values of another thread?

I can use a lock but after the parameters have been assigned. If the parameters are stored in the stack I guess they will live inside each threads stack but if they live in heap how can avoid threads changing another threads function parameters?

7
  • 1
    Can you illustrate your concerns a bit more? Are we talking about locals inside a function here, or the arguments to you pass to a function when you are calling it? Commented Jan 29, 2016 at 15:25
  • 1
    I'm not sure if you're aware, but different executions of a function won't "share" references to the same parameters (unless you're explicitly calling them with referentially identical arguments). This is true even for single-threaded programs, and is necessary to permit recursive functions to operate properly. For instance, def fib(x): return 1 if x <= 1 else fib(x-1) + fib(x-2) would give strange results if values of x lower in the call stack overwrote values of x higher up in the call stack. Commented Jan 29, 2016 at 15:37
  • @MartijnPieters I am talking about the arguments I pass to a function when calling it. Commented Feb 15, 2016 at 16:48
  • @Kevin I don't understad this "(unless you're explicitly calling them with referentially identical arguments)" can you explain more? Thanks. Finally you tell me that it doesn't matter if its a single thread function call or a function call shared across several threads, every call has its own memory space for its parameter values? Commented Feb 15, 2016 at 16:52
  • @Lben: I'm still not certain where you expect there to be a race condition? No mutable is safe from mutation by other threads. Are you worried that two calls to the same function will have their parameters mixed up? Commented Feb 15, 2016 at 16:57

1 Answer 1

6

Function parameters are put on the stack, and each thread has its own stack. You don't have to worry about their thread-safety.

However, all Python objects are stored on the heap; the stack merely holds references to such objects. If multiple threads are accessing one such mutable object they can still interfere with one another if the access is not synchronised somehow. This has nothing to do with how functions are called however.

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

7 Comments

Thank you can you point out some resource where this is stated? Thanks
Can you first respond to my query on your question? I'd like to give you the right resources.
I have answered your question, sorry for the delay.
Thank you, so, for instance, why in Java some methods are synchronized? Its because inside of them are references to objects in the heap?
@Lben: that's nothing to do with how methods are called and how parameters are passed to a function call. It just means that any call to that method locks that instance; any other calls to synchronized methods on that object must wait until the lock is freed.
|

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.