What kind of code can be called “re-entrant”?

前端 未结 9 1238
时光说笑
时光说笑 2020-12-24 02:34

Could someone tell me what code can be called \"re-entrant\" code?

I came across this word when reading some real time operating system. What disciplines must be st

相关标签:
9条回答
  • 2020-12-24 02:44

    Simply saying, a re-entrant code is a code which can be shared among multiple process.

    This is possible when the following conditions are satisfied:

    1. It should not have global and static data.
    2. It should not modify it's own code.
    3. It should not call another re-entrant function or code segment.

    So, a code following these conditions can be called re-entrant code.

    0 讨论(0)
  • 2020-12-24 02:48

    In general, a re-entrant block of code is one that can be entered by another actor before an earlier invocation has finished, without affecting the path that the first actor would have taken through the code. That is, it is possible to re-enter the code while it's already running and still produce correct results.

    In most cases, the "actors" are threads of the same process, but the concepts of thread safety and re-entrant are subtly different: not every thread-safe block is re-entrant, but every re-entrant block is thread-safe. That is, re-entrancy is a stronger property than thread safety. Here's a good example from Raymond Chen of how a block of code might be thread-safe but not re-entrant.

    There's a special case when the code is recursive: the same actor is calling into the code before its own invocation is finished, as Marc Gravell points out. All correct recursive blocks are re-entrant; of course, not every re-entrant block is recursive.

    0 讨论(0)
  • 2020-12-24 02:48

    A computer program is called reentrant if it can be interrupted in the middle of its execution and then safely called again before its previous invocations complete execution. The interruption could be caused by an internal action such as a jump or call, or by an external action such as a hardware interrupt or signal. Once the reentered invocation completes, the previous invocations will resume correct execution.

    0 讨论(0)
  • 2020-12-24 02:52

    Can another thread call the code while a first thread is in the middle of running it? If the code yields to a callback function, can the callback function itself call the code before the first runthrough has completed?

    If the code uses global vars that aren't locked down, or has its own static vars that it doesn't take special precautions with, any of those scenarios might break it.

    0 讨论(0)
  • 2020-12-24 02:56

    John Feminella's answer says:

    a re-entrant block of code is one that can be entered by another actor before an earlier invocation has finished. That is, it is possible to re-enter the code while it's already running.

    But that is also true of non-re-entrant block of code. If the block of code has been written without regard to this issue, it will still be possible for a second actor to enter it simultaneously.

    The issue is what effect this has on the results of either invocation. So more accurately: a re-entrant block is one that can be entered by another actor before an earlier invocation has finished, without changing the outcome of either invocation.

    Neither invocation should be able to detect the "presence" of the other.

    0 讨论(0)
  • 2020-12-24 02:57

    Re-entrant code is when the pages share a common resource and the resource should not be changed or manipulated. Then this resource is known as re-entrant code or pure code.

    0 讨论(0)
提交回复
热议问题