-2

Take the following code using thread_local variable as an example:

void func() {
    thread_local int a;
    ......
}

According to Wikipedia, I know it is a thread_safety function. But should we call it also a re-entrant function?

Nan Xiao
  • 16,671
  • 18
  • 103
  • 164
  • Note that normal local variables are stored on the stack and no 'thread local' modifier is needed to make those thread safe. Thread local storage would be relevant to file scope ('global') variables, or block-scope `static` variables — it isn't really relevant in the example code. That is, the function `func()` would be equally thread-safe and re-entrant (or not) if the C11 `thread_local` storage class was omitted. – Jonathan Leffler May 22 '17 at 05:38
  • @JonathanLeffler does not the 'thread_local' specifier make the function non-reentrant in the thread? Like Highlander, there can only be one 'thread_local int a;'? – ThingyWotsit May 22 '17 at 09:26

1 Answers1

0

Thread safety != re-entrancy.

A re-entrant function can be interrupted (by an interrupt or signal handler, say) and then re-entered by that signal handler; this happens in one thread.

We wouldn't be able to tell whether func() is re-entrant or not solely from the fact that it uses one thread-local variable.

To make a long story short: If a function accesses global data in a non-atomic way (or calls a function that does so), then it's non-reentrant. This doesn't necessarily involve threading at all.