Reputation: 357
I have a class method isConnected(socket soc) which will return true if the socket is connected or false if isn't.I want to call it in two running thread with the same thread function. is it not safe?
since I know that the method itself is reside in memory and it's share along the thread, so if one thread call isConnected method and it's running the other thread must not call it until the first thread finish with the method.
and I passing variable socket from thread to the isConnected method
Upvotes: 1
Views: 515
Reputation: 12616
Unless the method uses some shared resources then it's safe because it will create local variables on its own stack and won't share it with the other thread.
But be aware that one thread can return true and the other false, because one can find a socket closed and the other can find it still open.
So it's not really a good idea to call it from two thread at one time.
Upvotes: 1