1

if I have this type of program level function

def returnInt(s): return int(float(s)) 

and it ended up being called by 2 separate threads at the same time would it be "thread safe" or do I need to add as a function in both thread classes e.g

class StepperControl(threading.Thread): .... def returnInt(self,s): return int(float(self.s)) .... class BounceControl(threading.Thread): .... def returnInt(self,s): return int(float(self.s)) .... 

Simon

4
  • Why shouldn't it be? It might not do what you think though, it will basically ignore the parameter you pass and return self.s. If you want it to return self.bla or s = "bla" you'd need to use getattr(self, s) instead. Commented Jul 7, 2013 at 10:38
  • 2
    It has no side effects and it doesn't reference any variable/memory outside its scope so it is inherently thread safe. Commented Jul 7, 2013 at 10:39
  • @tibor - Ta :) Is you add you answer as an answer instead of a comment then I can mark up it up as accepted :) Commented Jul 7, 2013 at 10:54
  • @filmor - forgive the wrong thread code syntax - I just threw it in as a pseudo example :) Commented Jul 7, 2013 at 10:55

1 Answer 1

3

Indeed, It is thread-safe, as it does not access or attempt to change anything outside itself.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.