Linux pthread_create 不起作用。传递参数 3 警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11253025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:08:11  来源:igfitidea点击:

pthread_create not working. passing argument 3 warning

clinuxubuntupthreads

提问by randomizertech

I am trying to create a thread and from what I remember this should be the right way to do it:

我正在尝试创建一个线程,据我所知,这应该是正确的方法:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 5

int SharedVariable =0;
void SimpleThread(int which)
{
    int num,val;
    for(num=0; num<20; num++){
        if(random() > RAND_MAX / 2)
            usleep(10);
        val = SharedVariable;
        printf("*** thread %d sees value %d\n", which, val);
        SharedVariable = val+1;
    }
    val=SharedVariable;
    printf("Thread %d sees final value %d\n", which, val);
}

int main (int argc, char *argv[])
{
   pthread_t threads[NUM_THREADS];
   int rc;
   long t;
   for(t=0; t< NUM_THREADS; t++){
      printf("In main: creating thread %ld\n", t);
      rc = pthread_create(&threads[t], NULL, SimpleThread, (void* )t);
      if (rc){
         printf("ERROR; return code from pthread_create() is %d\n", rc);
         exit(-1);
      }
   }

   /* Last thing that main() should do */
   pthread_exit(NULL);
}

And the error that I'm getting is this one:

我得到的错误是这个:

test.c: In function ‘main': test.c:28: warning: passing argument 3 of ‘pthread_create' from incompatible pointer type /usr/include/pthread.h:227: note: expected ‘void * (*)(void *)' but argument is of type ‘void (*)(int)'

test.c:在函数'main'中:test.c:28:警告:从不兼容的指针类型/usr/include/pthread.h:227传递'pthread_create'的参数3:注意:预期'void * (* )( void *)' 但参数的类型是 'void (*)(int)'

I cannot change the SimpleThread function so changing the type of the parameter is not an option even though I already tried and it didn't work either.

我无法更改 SimpleThread 函数,因此更改参数的类型不是一个选项,即使我已经尝试过并且它也不起作用。

What am I doing wrong?

我究竟做错了什么?

采纳答案by dasblinkenlight

SimpleThreadshould be declared as

SimpleThread应该声明为

void* SimpleThread(void *args) {
}

When you pass parameters to your thread, it is best to define a structfor them, pass a pointer to that structas void*, and cast back to the right type inside the function.

将参数传递给线程时,最好struct为它们定义 a ,将指向该参数的指针传递给structas void*,然后转换回函数内部的正确类型。

回答by Sean Bright

Here's a compiling and "working" version of your program, although I have to admit to not knowing exactly what it's doing. For the critics in the audience, I apologize in advance for the pthread_joinloop at the end.

这是您程序的编译和“工作”版本,尽管我不得不承认不知道它在做什么。对于观众中的批评者,我提前为pthread_join结尾的循环道歉。

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

#define NUM_THREADS 5

struct my_thread_info {
    long which;
};

int SharedVariable = 0;

void *SimpleThread(void *data)
{
    int num, val;
    struct my_thread_info *info = data;

    for (num = 0; num < 20; num++) {
        if (random() > RAND_MAX / 2)
            usleep(10);
        val = SharedVariable;
        printf("*** thread %ld sees value %d\n", info->which, val);
        SharedVariable = val + 1;
    }

    val = SharedVariable;
    printf("Thread %ld sees final value %d\n", info->which, val);

    free(info);
    return NULL;
}

int main(int argc, char *argv[])
{
    pthread_t threads[NUM_THREADS];
    int rc;
    long t;
    struct my_thread_info *info;
    for (t = 0; t < NUM_THREADS; t++) {
        printf("In main: creating thread %ld\n", t);

        info = malloc(sizeof(struct my_thread_info));
        info->which = t;

        rc = pthread_create(&threads[t], NULL, SimpleThread, info);
        if (rc) {
            printf("ERROR; return code from pthread_create() is %d\n", rc);
            exit(-1);
        }
    }

    for (t = 0; t < NUM_THREADS; t++) {
        pthread_join(threads[t], NULL);
    }
}