释放线程内存
创建线程时,它会为局部变量等分配自己的存储空间。何时或如何将其释放回内存以供重用:线程何时结束?
当线程尝试分配动态存储时,它仍然存储在线程的本地存储中还是存储在全局存储中?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
线程的本地存储来自堆栈,因此不需要释放它。只要结束线程就可以了。
而动态存储是来自堆,所以必须释放分配的内存。
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.
请记住 - 还有“线程本地存储”(又名“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.
好吧,这取决于。如果线程是分离的,则线程结束时不需要清理线程的资源。如果线程没有分离,您可能(我不确定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.
线程在堆栈上为函数数据分配本地存储,或者在特殊的 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.