c - pthread_join 参数类型错误

标签 c pthreads pthread-join

我一直在尝试创建一个从标准输入读取并将输入存储到链表的简单线程。创建和加入线程时出现以下错误:

warning: passing argument 1 of ‘pthread_join’ makes integer from pointer without a cast [-Wint-conversion]
   pthread_join(prod_thread, NULL);

我正在将一个 thread_t* 参数传递给 pthread_join,但它似乎需要一个 int,这让我感到困惑。谁能解释为什么会这样?这是代码:

pair_t* head = malloc(sizeof(pair_t));
pthread_t* prod_thread = malloc(sizeof(pthread_t));
pthread_create(prod_thread, NULL, prod_func, head);
pthread_join(prod_thread, NULL);

prod_func 函数如下所示:

void* prod_func(void* head) {
  ...
}

我也试过调用 pthread_join(&prod_thread, NULL); 但是我得到了同样的错误。

最佳答案

I'm passing a thread_t* argument to pthread_join but it seems that it expects an int which confuses me.

pthread_join期望它的第一个参数是 pthread_t(不是 pthread_t *)。 pthread_t 的确切类型因实现而异,但在您的实现中它是整数类型。您正在传递一个指针。

I've also tried calling pthread_join(&prod_thread, NULL); but then I get the same error.

当然可以。如果 prod_thread 的类型为 pthread_t *,则其地址 &prod_thread 的类型为 pthread_t **。这是错误的方向(结果仍然是一个指针)。当您编写声明时,您真正想要的是

pthread_join(*prod_thread, NULL);

关于c - pthread_join 参数类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58589515/

相关文章:

c - 为什么我的代码在 Visual Studio 中返回 -nan,但在 Linux 中不返回?

C 函数第二次运行时抛出异常

C 通过指针将数组传递给函数

c++ - pthread_mutex 锁是否比用户在代码中强加的内存屏障提供更高的性能

从主函数外部调用 pthread_create

使用 C 计算段落中的单词数

c++ - C++类网络编程的段错误

c - 在 while 循环检查中处理互斥变量

c - 使用 pthread_join 时出现段错误(退出代码 139)

c - 文件名未传递到 pthread_create