c - 线程与其父栈之间的关系是什么?

标签 c linux multithreading pthreads

pthreads 都有自己的堆栈并共享堆。它们在创建时继承父级的各个方面(sig 掩码、fpenv 等)。子线程和父栈的关系好像很少说。

那么我是否可以通过 pthread_create 将父堆栈上某些内容的地址传递给子线程并期望它起作用?子线程能不能看到父进程的栈?子堆栈是否在创建时填充了父堆栈的副本?

了解标准保证的内容以及实际可行的内容对我很有用。

最佳答案

So can I pass the address of something on the parent stack to a child thread via pthread_create and expect this to work? Is the child thread able to see the stack of the parent process?

实际上是的。几乎所有的 pthreads 实现都支持它。如果“父”线程在新创建的线程可能仍在使用父线程的堆栈地址之前没有完成,这很好。但标准实际上并不要求它。将堆栈地址(自动变量的地址)从“父”线程传递到新创建的线程被称为实现定义。因此,为了绝对确定,您需要动态分配(使用 malloc 和 friend )并将其传递给 pthread_create()

Does the childs stack get populated with a copy of the parent stack at creation time?

这又是实现定义的。可以通过复制父堆栈或分配单独的堆栈来创建新线程。两者都是有效的,并且没有要求以一种或另一种方式进行。

参见 [pthread_create()] 的基本原理部分 1获取相关信息。

关于c - 线程与其父栈之间的关系是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43136881/

相关文章:

c - 二进制文件写入问题

sql - 如何在 SQLite 中使用阿拉伯逗号选择 SQL

linux - 根据条件替换值并使用 AWK 比较多个文件中的值

php - fedora 20 上的 httpd (apache2) 权限错误

c++ - 在各自的线程上运行多个对象似乎多次运行同一个对象

c - 尝试将 char 指针附加到固定字符串时出现内存异常

C程序: Error reading and printing a particular column from a text file

linux - 将信号捕获到进程组

java - 从多个线程修改 ArrayAdapter 时如何防止 ConcurrentModificationException?

java - 如何使用数据提供程序在 testNG 上并行运行测试?