UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » reentrant function & threadsafe function

#1 2007-09-13 04:19 AM

zhn636
Member
Registered: 2007-04-24
Posts: 146

Re: reentrant function & threadsafe function

1.whether the reentrant function is the same as threadsafe function?
2.how can we make a non-reentrant function to become a reentrant function?
3.when we handle the signal ,whether we must use the reentrant function.
thanks
  best wishes!

Offline

#2 2007-09-13 12:14 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: reentrant function & threadsafe function

1. No, a re-entrant function can be called multiple times at the same time, but that isn't necessarily by different threads. E.g. recursion can cause it too, or signal handling code.

2. By not using any external globally visible memory. So no global vars or static data, and when doing following given pointers it shouldn't modify anything.

3. No, but the functions you call should be re-entrant. Only oen signal handler is called at the time (for multithreaded code this might not be true).

Offline

#3 2007-09-13 02:25 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: reentrant function & threadsafe function

Just to expound a bit (or maybe confuse you more ;-)) on what i3839 said about
#1: Every reentrant function is thread-safe by definition, but NOT every thread-safe
function is necessarily reentrant...  Because, you can acheive thread-safety via
other means (locks, atomic ops, etc.), but you can only acheive reentrancy by
avoiding all global/static memory completely...

Offline

#4 2007-09-13 05:27 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: reentrant function & threadsafe function

To make things more confusing: A re-entrant function can be thread save, but can also be used in an unsafe way if it takes a pointer to a shared data structure that can be modified by another thread.

Offline

#5 2007-09-14 01:08 AM

zhn636
Member
Registered: 2007-04-24
Posts: 146

Re: reentrant function & threadsafe function

A re-entrant function can be used in an unsafe way ,ant at this moment ,whether this re-entrant function is not threadsafe .

Offline

  • Index
  • » C
  • » reentrant function & threadsafe function

Board footer

Powered by FluxBB