堆分配内存的线程安全

发布于 2024-09-01 22:58:24 字数 616 浏览 14 评论 0原文

我正在读这篇文章: http://en.wikipedia.org/wiki/Thread_safety

如下函数线程安全?

void foo(int y){
    int * x = new int[50];
    /*...do some stuff with the allocated memory...*/
    delete [] x;
}

文章中说,为了线程安全,您只能使用堆栈中的变量。真的吗?为什么?后续调用上述函数不会在其他地方分配内存吗?

编辑:啊。看来我误读了文章的这一部分:

子例程是可重入的,因此是线程安全的,如果

  • 它使用的唯一变量来自堆栈

(我认为它的意思是

子例程是可重入的,因此是线程安全的,当且仅当

  • 它使用的唯一变量来自堆栈

,根据下面的答案,情况并非如此)

I was reading this: http://en.wikipedia.org/wiki/Thread_safety

Is the following function thread-safe?

void foo(int y){
    int * x = new int[50];
    /*...do some stuff with the allocated memory...*/
    delete [] x;
}

In the article it says that to be thread-safe you can only use variables from the stack. Really? Why? Wouldn't subsequent calls of the above function allocate memory elsewhere?

Edit: Ah. Looks like I misread this part of the article:

A subroutine is reentrant, and thus thread-safe, if

  • the only variables it uses are from the stack

(I took it to mean

A subroutine is reentrant, and thus thread-safe, if and only if

  • the only variables it uses are from the stack

, which according to the answers below, is not the case)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖心男生 2024-09-08 22:58:24

如果您在支持多线程的环境中进行编码,那么您可以非常确定 new 是线程安全的。

虽然内存在堆上,但指向它的指针却在栈上。只有您的线程拥有指向此内存的指针,因此不存在并发修改的风险 - 没有其他线程知道内存在哪里修改它。

如果您将此指针传递给另一个线程,然后该线程与原始(或另一个)线程同时修改此内存,那么您只会遇到线程安全问题。

If you are coding in an environment that supports multi-threading, then you can be pretty sure new is thread safe.

Although the memory is on the heap, the pointer to it is on the stack. Only your thread has the pointer to this memory, and so there is no risk of concurrent modification - no other thread knows where the memory is to modify it.

You would only get a problem with thread safety if you were to pass this pointer to another thread that would then concurrently modify this memory at the same time as your original (or another) thread.

无妨# 2024-09-08 22:58:24

它并不是说只能使用堆栈变量,而是说使用堆变量“表明需要仔细检查以查看它是否不安全”。

newdelete 通常以线程安全的方式实现(尽管不确定标准是否保证),所以上面的代码可能没问题。

另外通常建议使用 std::vector 而不是手动分配数组,但我假设您仅提供作为示例:)

It doesn't say you can only use stack variables, it says that using heap variables "suggests the need for careful examination to see if it is unsafe".

new and delete are normally implemented in a threadsafe way (not sure that the standard guarantees that though) so your code above will probably be fine.

Plus usual recommendations of using std::vector instead of manually allocating an array, but I assume you only provided that as an example :)

暮年 2024-09-08 22:58:24

new 和delete 可能是也可能不是线程安全的。它们可能是,但这取决于实现。看:
Linux 和 gcc 4 中的 C++ 新运算符线程安全

为了保证线程安全,函数必须使用堆栈变量或将其对其他资源的访问与其他线程同步。只要从不同线程调用时单独调用 new 在堆上分配不同的空间,就应该没问题。

new and delete may or may not be thread safe. They probably are, but that is implementation-dependent. See:
C++ new operator thread safety in linux and gcc 4

To be thread safe, a function must either use stack variables or synchronize its access to other resources with other threads. As long as separate calls to new allocate different space on the heap when called from different threads, you should be fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文