本文介绍了线程VS重入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我问一个问题,与标题和里面的那个我问:是的malloc重入?

Recently, I asked a question, with title as "Is malloc thread safe?", and inside that I asked, "Is malloc re-entrant?"

我是IM pression所有再入是线程安全下。

I was under the impression that all re-entrant are thread-safe.

这是假设错了?

推荐答案

重入函数不依赖于(),它被暴露在C库头..采取的strtok()VS strtok_r例如在C全局变量

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.

某些功能需要存储一个正在进行的工作的地方,重入功能允许您将线程自己的存储中指定该指针,而不是在全球性的。

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.

错误号,然而,是对POSIX系统情况会稍有不同:)

errno, however, is a slightly different case on POSIX systems :)

总之,折返的往往的意思线程安全的(如使用该功能的折返版本,如果你使用的线程),但线程安全并不总是意味着重入。有些功能不依赖于暴露的一些全局变量,其他线程可能破坏。

In short, reentrant often 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. Some functions do not rely on some exposed global variable that other threads could clobber.

的malloc()有没有必要重入的,它不依赖于任何可能的入口点的范围对于任何给定主题。

malloc() has no need to be reentrant, it does not depend on anything out of the scope of the entry point for any given thread.

这是返回静态分配的值的函数是的的线程安全的,而无需使用一个互斥体,futex的,或其他原子锁机制。然而,他们并不需要为可重入。

Functions that return statically allocated values are not thread safe without the use of a mutex, futex, or other atomic locking mechanism. Yet, they don't need to be reentrant.

即:

static char *foo(unsigned int flags)
{
  static char ret[2] = { 0 };

  if (flags & FOO_BAR)
    ret[0] = 'c';
  else if (flags & BAR_FOO)
    ret[0] = 'd';
  else
    ret[0] = 'e';

  ret[1] = 'A';

  return ret;
}

所以,你可以看到,有多个线程使用没有某种锁定将是一场灾难的..但它没有目的是重入的。你会碰到,当动态分配的内存是一些嵌入式平台的大忌。

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.

在纯函数式编程,折返经常的的暗示线程安全的,这将取决于传递给函数的入口点定义或匿名函数,递归等行为

In purely functional programming, reentrant often doesn't imply thread safe, it would depend on the behavior of defined or anonymous functions passed to the function entry point, recursion, etc.

有一个更好的方式把线程安全为 安全的并发访问,这能更好地解释的必要。

A better way to put 'thread safe' is safe for concurrent access , which better illustrates the need.

这篇关于线程VS重入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 22:29