Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Re-entrant functions do not rely on global variables that are exposed in the C library headers .. take strtok() vs strtok_r() for example in C.</p> <p>Some functions need a place to store a 'work in progress' , re-entrant functions allow you to specify this pointer within the thread's own storage, not in a global. Since this storage is exclusive to the calling function, it can be interrupted and <em>re-entered</em> (re-entrant) and since in most cases mutual exclusion beyond what the function implements isn't required for this to work, they are often considered to be <em>thread safe</em>. This isn't, however, guaranteed by definition. </p> <p>errno, however, is a slightly different case on POSIX systems (and tends to be the oddball in any explanation of how this all works) :)</p> <p>In short, reentrant <em>often</em> means thread safe (as in "use the reentrant version of that function if you're using threads"), but thread safe does not always mean re-entrant (or the reverse). When you're looking at thread-safety, <em>concurrency</em> is what you need to be thinking about. If you have to provide a means of locking and mutual exclusion to use a function, then the function isn't inherently thread-safe. </p> <p>But, not all functions need to be examined for either. <code>malloc()</code> has no need to be reentrant, it does not depend on anything out of the scope of the entry point for any given thread (and is itself thread safe). </p> <p>Functions that return statically allocated values are <em>not</em> thread safe without the use of a mutex, futex, or other atomic locking mechanism. Yet, they don't need to be reentrant if they're not going to be interrupted. </p> <p>i.e.:</p> <pre><code>static char *foo(unsigned int flags) { static char ret[2] = { 0 }; if (flags &amp; FOO_BAR) ret[0] = 'c'; else if (flags &amp; BAR_FOO) ret[0] = 'd'; else ret[0] = 'e'; ret[1] = 'A'; return ret; } </code></pre> <p>So, as you can see, having multiple threads use that without some kind of locking would be a disaster .. but it has no purpose being re-entrant. You'll run into that when dynamically allocated memory is taboo on some embedded platform.</p> <p>In purely functional programming, reentrant often <em>doesn't</em> imply thread safe, it would depend on the behavior of defined or anonymous functions passed to the function entry point, recursion, etc. </p> <p>A better way to put 'thread safe' is <strong><em>safe for concurrent access</em></strong> , which better illustrates the need. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload