小编m-a*_*olf的帖子

pthread_join 破坏了堆栈中的 pthread_create 参数

我从一本高级 Linux 编程书中得到了这段代码。当我尝试在 Linux 64 位环境下执行代码时which_primepthread_join()函数调用后变量的值会损坏(更改为 0)。

在此示例中,为什么which_prime在运行 pthread_join 后会损坏的值?

一般来说,即使我们调用其他函数,我们是否可以在 main 中安全地使用传递给 pthread_create 函数的第 4 个参数pthread_join()

#include <pthread.h>
#include <stdio.h>

/* Compute successive prime numbers (very inefficiently). Return the
Nth prime number, where N is the value pointed to by *ARG. */
void* compute_prime (void* arg)
{
    int candidate = 2;

    int n = *((int*) arg);
    while (1) {
        int factor;
        int is_prime = 1;
        /* Test primality by successive division. …
Run Code Online (Sandbox Code Playgroud)

c linux multithreading

5
推荐指数
1
解决办法
610
查看次数

标签 统计

c ×1

linux ×1

multithreading ×1