2

Like the title says:

If I have a class with a static member function, which by itself contains no static variables, can I consider the member function reentrant?

Cantfindname
  • 2,008
  • 1
  • 17
  • 30

3 Answers3

4

Static member functions are no different from namespace-scope functions (or even member functions) with respect to reentrancy. They are not predisposed either way, it depends entirely on what the function does inside.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
4

The rules for Reentrancy laid out in the Wikipedia article on Reentrancy are as follows:

  • Reentrant code may not hold any static (or global) non-constant data.
  • Reentrant code may not modify its own code.
  • Reentrant code may not call non-reentrant computer programs or routines.

so as long as the member function follows those rules it should be ok.

The article Use reentrant functions for safer signal handling I feel gives a slightly better desciption:

A reentrant function is one that can be used by more than one task concurrently without fear of data corruption. Conversely, a non-reentrant function is one that cannot be shared by more than one task unless mutual exclusion to the function is ensured either by using a semaphore or by disabling interrupts during critical sections of code. A reentrant function can be interrupted at any time and resumed at a later time without loss of data. Reentrant functions either use local variables or protect their data when global variables are used.

A reentrant function:

  • Does not hold static data over successive calls
  • Does not return a pointer to static data; all data is provided by the caller of the function
  • Uses local data or ensures protection of global data by making a local copy of it
  • Must not call any non-reentrant functions

and then goes on to explain how this differs from thread-safety before going the article fully.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
0

If your static member function contains only auto variables you could consider it re-entrant.

sanjayk79
  • 542
  • 3
  • 15