c - 警告 : passing argument 1 of ‘pthread_join’ makes integer from pointer without a cast error

标签 c pthreads posix

#include  pthread.h
#include stdio.h

static int seq[50];
void *runner(void *); /* the thread */

int main(int argc, char *argv[])
{
    int y;
    pthread_t tid[3]; /* the thread identifier */
    pthread_attr_t attr; /* set of attributes for the thread */
    if(argc!=2)
    {
        fprintf(stderr,"you need to enter two arguments");
        exit(-1);
    }
    else
    {
        int a = atoi(argv[1]);
        if(a>0)
        {
            y=a;
        }
        else
        {
            fprintf(stderr,"you need to enter a valid number greater than zero");
            exit(-1);
        }
    }
    /* get the default attributes */
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
    /* create three threads */
    pthread_create(&tid, NULL, runner,(void *)y);
    /* now wait for the thread to exit */
    sleep(2);
    printf( " Am in main process, The sequence is  ");
    int j=0,val=0;
    for( j = 0; j < 40; j++)
    {
        val=seq[j];
        if(val>=1)
            printf( "  %d  ", seq[j]);
    }
    pthread_join(tid, NULL);
    pthread_exit(0);
}
/**
 * The thread will begin control in this function
 */
void *runner(void *param)
{
    int count=0;
    int y= (int *) param;
    //       printf(" Am in runner");
    while(y!=1)
    {
        if(y%2==0)
            y = y/2;
        else
            y= ((3*y)+1);
        //  printf(" %d ", y);
        seq[count] = y;
        count++;

    }
    pthread_exit(0);
}

我收到以下错误

bonus.c: In function ‘main’:
bonus.c:91: warning: cast to pointer from integer of different size
bonus.c:91: warning: passing argument 1 of ‘pthread_create’ from incompatible pointer type
bonus.c:123: warning: passing argument 1 of ‘pthread_join’ makes integer from pointer without a cast
bonus.c: In function ‘runner’:
bonus.c:157: warning: comparison between pointer and integer
bonus.c:159: error: invalid operands to binary %
bonus.c:161: error: invalid operands to binary /
bonus.c:165: error: invalid operands to binary *
bonus.c:171: warning: assignment makes integer from pointer without a cast

最佳答案

无论出于何种原因,您都声明了一个 pthread_t 数组:

pthread_t tid[3];

不要这样做,除非您计划启动三个线程。
你想要

pthread_t tid;

由此产生两个错误:

pthread_create(&tid, NULL, runner,(void *)y);
(passing argument 1 of ‘pthread_create’ from incompatible pointer type)

其中传递指向数组的指针而不是指向 pthread_t 的指针,并且

pthread_join(tid, NULL);
(passing argument 1 of ‘pthread_join’ makes integer from pointer without a cast)

在其中传递指向 pthread_t 而不是 pthread_t 的指针。

警告

cast to pointer from integer of different size

来自 (void*) y — 如果您针对 64 位进行编译,则 int 小于 void*目标。
您应该使用 &y 或为 y 指定一个与 void* 一样大的类型,例如 int64_t

runner 中,您将参数转换为 int*,这很奇怪,因为它一开始是 int
您需要确保转换回与开始时相同的类型。
然后,您继续使用该值初始化 int,但这不可能是您的实际代码,因为以下错误与

一致
int* y = (int *) param;

所以我怀疑您在问题中输入代码时丢失了“*”。

如果您在参数中传递一个指向 int 的指针,则应该是

int y = *(int*) param;

如果您传递一个重新解释为指针的整数,它应该是

int64_t y = (int64_t) param;

如果您选择 int64_t 作为整数类型。

此外,请记住,在线程修改数组元素时打印它们可能不是最好的主意,而且也不能确保线程不会超出数组边界。

关于c - 警告 : passing argument 1 of ‘pthread_join’ makes integer from pointer without a cast error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31082559/

相关文章:

c - 是否可以从 FILE* 中拯救文件描述符?

c - 管道 - 与多个 fork 的子进程通信

c - S_IFMT 和 S_IFREG 未定义为 -std=c11 或 -std=gnu11

c - C中链表的标准实现

c - 如何使用 tcc 和 tc 在 Dos 提示符下编译 C 程序

c - 关于宏

c - 如果函数中已有线程,如何防止线程进入该函数?

c++ - 仅使用 get 或 set 的原始类型的线程安全威胁是否被夸大了?

c++ - NSOperationQueue 与 pthread 优先级

c++ - 在处理新的 pthread 时将对象的实例作为参数传递