Reentrant

From Wikipedia, the free encyclopedia

(Redirected from Re-entrant)
Jump to: navigation, search
For other uses of the term, see Reentrant (disambiguation)

A computer program or routine is described as reentrant if it can be safely executed concurrently; that is, the routine can be re-entered while it is already running. To be reentrant, a function must:

  • Hold no static (global) non-constant data.
  • Must not return the address to static (global) non-constant data.
  • Must work only on the data provided to it by the caller.
  • Must not rely on locks to singleton resources.
  • Must not call non-reentrant functions.

Multiple levels of 'user/object/process priority' and/or multiprocessing usually complicate the control of reentrant code. Also, IO code is usually not reentrant because it relies on shared, singleton resources such as disks.

Reentrancy is a key feature of functional programming.

Contents

In the following piece of C code, neither functions f nor g are reentrant.

int g_var = 1;

int f()
{
  return g_var + 2;
}

int g()
{
  return f() + 2;
}

int main()
{
  g();
  return 0;
}

In the above, f depends on a global variable g_var; thus, if two threads execute it and access g_var concurrently, then the result varies depending on the timing of the execution. Hence, f is not reentrant. Neither is g; it calls f, which is not reentrant.

These slightly-altered versions are reentrant:

int f(int i)
{
  return i + 2;
}

int g(int i)
{
  return f(i) + 2;
}

int main()
{
  g(1);
  return 0;
}

Reentrance and thread-safety are separate concepts: a function can be reentrant, thread-safe, both, or neither. However, both these concepts are related to the way functions handle resources.

A thread-safe function protects shared resources from concurrent access by locks. Thread-safety concerns only the implementation of a function and does not affect its external interface. In case of reentrancy, the external interface itself should be such that all data is provided by the caller of the function.

In most cases, to make a non-reentrant function reentrant, it must be replaced by a function with a modified interface. To make a thread-unsafe function thread-safe, only the implementation needs to be changed, usually by adding synchronization blocks.

Non-reentrant functions are not thread-safe. Furthermore, it may be impossible to make a non-reentrant function thread-safe.

Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.