Questions tagged [reentrancy]

Reentrancy usually refers to subroutines, functions, methods and mutexes. A subroutine is considered reentrant if it can be safely called before a previous call has completed.

To be reentrant a subroutine, function, method etc must:

  • hold no static (or global) non-constant data
  • not return the address to static (or global) non-constant data
  • work only on the data provided to it by the caller
  • not rely on locks to singleton resources

A reentrant mutex's lock can be acquired multiple times by the same thread. However, the lock must be released the same number of times or else other threads will be unable to acquire the lock. It has some similarities to a counting semaphore. More info here: Reentrant mutex

See also

214 questions
252
votes
8 answers

What exactly is a reentrant function?

Most of the times, the definition of reentrance is quoted from Wikipedia: A computer program or routine is described as reentrant if it can be safely called again before its previous invocation has been completed (i.e it can be safely…
Lazer
  • 90,700
  • 113
  • 281
  • 364
116
votes
3 answers

What is the difference between Lock and RLock

From the docs: threading.RLock() -- A factory function that returns a new reentrant lock object. A reentrant lock must be released by the thread that acquired it. Once a thread has acquired a reentrant lock, the same thread may acquire it again…
BufBills
  • 8,005
  • 12
  • 48
  • 90
104
votes
3 answers

Threadsafe vs re-entrant

Recently, I asked a question, with title as "Is malloc thread safe?", and inside that I asked, "Is malloc re-entrant?" I was under the impression that all re-entrant are thread-safe. Is this assumption wrong?
Alphaneo
  • 12,079
  • 22
  • 71
  • 89
50
votes
6 answers

What is the meaning of "ReentrantLock" in Java?

Reentrancy means that locks are acquired on a per-thread rather than per-invocation basis. Since an intrinsic lock is held by a thread, doesn't it mean that a thread run once equals an invocation basis? Thank you, it seems mean that: in a thread,if…
znlyj
  • 1,109
  • 3
  • 14
  • 34
44
votes
6 answers

Why are malloc() and printf() said as non-reentrant?

In UNIX systems we know malloc() is a non-reentrant function (system call). Why is that? Similarly, printf() also is said to be non-reentrant; why? I know the definition of re-entrancy, but I wanted to know why it applies to these functions. …
ultimate cause
  • 2,264
  • 4
  • 27
  • 44
34
votes
1 answer

Does SemaphoreSlim (.NET) prevent same thread from entering block?

I have read the docs for SemaphoreSlim SemaphoreSlim MSDN which indicates that the SemaphoreSlim will limit a section of code to be run by only 1 thread at a time if you configure it as: SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1,…
Dave
  • 8,095
  • 14
  • 56
  • 99
31
votes
1 answer

Is the volatile keyword required for fields accessed via a ReentrantLock?

My question refers to whether or not the use of a ReentrantLock guarantees visibility of a field in the same respect that the synchronized keyword provides. For example, in the following class A, the field sharedData does not need to be declared…
Matthew Blackford
  • 3,041
  • 1
  • 24
  • 28
26
votes
2 answers

Code Re-entrancy vs. Thread Safety

What is the difference between the concepts of "Code Re-entrancy" and "Thread Safety"? As per the link mentioned below, a piece of code can be either of them, both of them or neither of them. Reentrant and Thread safe code I was not able to…
Codex
  • 1,022
  • 2
  • 17
  • 25
25
votes
2 answers

How to explain the reentrant RuntimeError caused by printing in signal handlers?

Code: # callee.py import signal import sys import time def int_handler(*args): for i in range(10): print('INTERRUPT', args) sys.exit() if __name__ == '__main__': signal.signal(signal.SIGINT, int_handler) …
hsfzxjy
  • 1,242
  • 4
  • 14
  • 22
22
votes
9 answers

what is the difference between re-entrant function and recursive function in C?

In C I know about the recursive function but I heard about the re-entrant function.What is that? And whats the difference between them?
Manoj Doubts
  • 13,579
  • 15
  • 42
  • 45
19
votes
3 answers

Is process in VHDL reentrant?

Is it possible two or more sequential run for a process in VHDL? What will happen if another event happen (on sensitivity signal list) while the sequential execution of a process is not completed ? Is it possible or my VHDL model in mind for…
mohtashami740
  • 336
  • 2
  • 6
18
votes
5 answers

Stopping timer in its callback method

I have a System.Threading.Timer that calls its appropriate event handler (callback) every 10 ms. The method itself is not reentrant and can sometimes take way longer than 10 ms. Thus, I want to stop the timer during method execution. Code: private…
Kornelije Petak
  • 9,412
  • 15
  • 68
  • 96
17
votes
1 answer

Qt documentation and reentrancy

The Qt documentation states this about thread-safety and reentrancy: Note: Qt classes are only documented as thread-safe if they are intended to be used by multiple threads. If a function is not marked as thread-safe or reentrant, it should not be…
sashoalm
  • 75,001
  • 122
  • 434
  • 781
16
votes
1 answer

Difference between thread safe and async-signal safe

According to APUE 2e Chapter 12.5: If a function is reentrant with respect to multiple threads, we say that it is thread-safe. This doesn't tell us, however, whether the function is reentrant with respect to signal handlers. We say that a function…
PickBoy
  • 1,234
  • 1
  • 13
  • 20
16
votes
3 answers

Mixing synchronized() with ReentrantLock.lock()

In Java, do ReentrantLock.lock() and ReetrantLock.unlock() use the same locking mechanism as synchronized()? My guess is "No," but I'm hoping to be wrong. Example: Imagine that Thread 1 and Thread 2 both have access to: ReentrantLock lock = new…
James Jensen
  • 363
  • 3
  • 8
1
2 3
14 15