0

我有一个与线程和进程的内存地址有关的问题。问题是:-在正常通话中

int func(int a, int b){
    int c;
    c = a + b;
    return c;
}         

int main(){
    int ret = func(a,b);
     return 0;
}

在上面对函数 func 的函数调用中,函数变量 a 和 b 将被存储在堆栈中。请纠正我,如果我错了。

现在另一种情况是当我们从主进程创建线程时。

void * func(void *dummy_ptr){
    int c;
    c = a + b;
    pthread_exit();
}         

int main(){
    pthread_t  id;
    int ret = pthread_create(&id, NULL, & func(), NULL);
    return 0;
}

我的问题是 pthread_create 的变量将存储在哪里。它是存储在主堆栈还是线程堆栈上。

4

2 回答 2

1

pthread_create在堆中为新线程的堆栈分配空间。所以里面的变量func都存储在线程的栈上,而栈本身就位于程序的堆中。

于 2013-06-12T20:26:17.633 回答
0

该变量pthread_t id是 main 的本地变量,因此它必须在 main 的堆栈上创建。

main完成执行时,

  • 您无需等待线程终止pthread_joinmain
  • 线程未分离。

main退出导致所有其他线程突然终止(杀死)。

于 2013-06-14T11:42:06.987 回答