简体   繁体   English

当线程退出时,线程中声明的变量会发生什么?

[英]What happens to variables declared in a thread when the thread exits?

Here is an example of a thread.这是一个线程的示例。


void* thrfun(void* arg)
{
    int var;
    var = 7;
    
    
    pthread_exit(NULL);
}

What happens to var when it exits. var 退出时会发生什么。 Is var de allocated, or is var still sitting in memory as a leak?是 var de 分配的,还是 var 仍然作为泄漏存在于内存中?

Is a thread function simply still a function, and all variables inside are local, meaning they get de allocated upon exit?线程函数是否仍然只是一个函数,并且内部的所有变量都是本地的,这意味着它们在退出时被取消分配?

The local variables declared in thrfun(void* arg) are placed on the thread's stack (if the system has a stack) and will be released as soon as the function returns, just like with any other function.thrfun(void* arg)中声明的局部变量被放置在线程的堆栈中(如果系统有堆栈),并且将在函数返回后立即释放,就像任何其他函数一样。

Is var de allocated, or is var still sitting in memory as a leak?是 var de 分配的,还是 var 仍然作为泄漏存在于内存中?

The memory claimed by var will be released. var占用的内存将被释放。

If you instead do manual memory allocation, with malloc for example, that memory remains allocated (on the heap, if the system has a heap) until you free it, also just like with any other function.如果您改为手动分配内存,例如使用malloc ,则该内存将保持分配状态(在堆上,如果系统有堆),直到您free它,就像使用任何其他函数一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM