我从一本高级 Linux 编程书中得到了这段代码。当我尝试在 Linux 64 位环境下执行代码时which_prime,pthread_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)