释放线程内存

发布于 2024-11-30 20:27:02 字数 102 浏览 2 评论 0原文

创建线程时,它会为局部变量等分配自己的存储空间。何时或如何将其释放回内存以供重用:线程何时结束?

当线程尝试分配动态存储时,它仍然存储在线程的本地存储中还是存储在全局存储中?

When creating a thread, it is allocated its own storage for local variables, etc. When or how is this released back to memory for re-use: when the thread ends?

When a thread tries to allocate a dynamic storage, is it still stored in the thread's local storage or is it stored in a global storage?

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

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

发布评论

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

评论(4

椵侞 2024-12-07 20:27:02

线程的本地存储来自堆栈,因此不需要释放它。只要结束线程就可以了。

而动态存储是来自堆,所以必须释放分配的内存。

The local storage for thread is from stack, so you don't need to release it. just end thread is ok.

And dynamic storage is from heap, so you must release the allocated memory.

故事↓在人 2024-12-07 20:27:02

请记住 - 还有“线程本地存储”(又名“TLS”)。这独立于堆存储(“new”或“malloc()”)或局部变量。与堆存储一样,您的应用程序将显式分配 TLS 并显式释放它。

Remember - there's also "thread local storage" (aka "TLS"). This independent of either heap storage ("new" or "malloc()") or local variables. Like heap storage, your application will explicitly allocate TLS and explicitly free it.

为你鎻心 2024-12-07 20:27:02

好吧,这取决于。如果线程是分离的,则线程结束时不需要清理线程的资源。如果线程没有分离,您可能(我不确定Solaris的具体情况)需要调用pthread_join或类似的东西来清理线程的资源。当然,您需要清理 TLS 和动态存储。

Well it depends. If the thread is detached, you don't need to clean up the thread's resources when the thread ends. If the thread isn't detached you may (I'm not sure about the Solaris specifics) need to call pthread_join or something like that to clean up the thread's resources. Of course, you'll need to clean up TLS and dynamic storage.

怪我入戏太深 2024-12-07 20:27:02

线程在堆栈上为函数数据分配本地存储,或者在特殊的 TLS(线程本地存储)中为线程本地全局变量分配本地存储。线程在堆上分配的任何内容都会保留下来,直到显式 free()d。

Threads allocate local storage on the stack for function data, or in a special TLS (Thread local storage) for thread-local globals. Anything allocated by the thread on the heap remains until explicitly free()d.

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