Thread Safe and Rentrancy


 
Thread Tools Search this Thread
Top Forums Programming Thread Safe and Rentrancy
# 1  
Old 10-26-2012
Thread Safe and Rentrancy

I have read at many places that a thread-safe function is always reentrant, but a reentrant function is not always thread-safe.

I understand the second part that reentrant function is not always thread-safe, since the reentrant function works on each of the thread's local data and so if the reentrant function is accessing any global data in it then it cannot be threadsafe.

But will anybody explain with example how can a threadsafe function be always reentrant?
# 2  
Old 10-26-2012
Have you looked at this: Wikipedia - Thread safety?
This User Gave Thanks to fpmurphy For This Post:
# 3  
Old 10-26-2012
Yes murphy I had looked at it.
But pls have a look at the following link The difference between thread-safety and re-entrancy - The Old New Thing - Site Home - MSDN Blogs

Last edited by rupeshkp728; 10-26-2012 at 12:39 PM.. Reason: added details
# 4  
Old 10-26-2012
Quote:
Originally Posted by rupeshkp728
But will anybody explain with example how can a threadsafe function be always reentrant?
Emm, don't think there is need for example. Thread safe function means it can be ran safely in same context by many threads so logically thinking its automatically safe to rerun many times this function in a single thread as well...

Function can not be thread safe if its not reentrant.
This User Gave Thanks to expl For This Post:
# 5  
Old 10-26-2012
That article is clear as mud. How in the world is one thread going to make two simultaneous calls?

So just forget about threads for the moment. Re-entrant means you could call AddToString in main(), and in the middle of it, your program gets SIGUSR1 or something, and you have an interrupt handler configured for it. The software interrupt freezes your program and executes its own function until done -- and it uses AddToString too. Both calls end up adding the character as the first in the string because the first call hadn't managed to update length yet.

A re-entrant subroutine has to be able to handle these situations gracefully. Often that's done by just avoiding the problem completely -- not using global data of any sort, which prevents both things from competing. If they're not stomping on each other's data it's safe. It wouldn't even need to be mutexed anymore.

Otherwise, you might need to use atomic operations in a very carefully considered way to guarantee neither stomps on the other's variables.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. Programming

compiling thread safe c on aix

Hi, I'm trying to compile a Perl module for AIX 5.3. Now, Perl by default wants to use cc_r as compiler but I only have gcc installed on this system. How can I get gcc to be thread safe? Or can I use gcc without any options and expect it to behave the same as cc_r? (0 Replies)
Discussion started by: rein
0 Replies
Login or Register to Ask a Question