请教一个在pthread线程函数中使用回调函数的问题

toongyu_zty 2018-04-28 04:59:07
以下就是想在线程函数中使用回调函数的代码,编译通过,运行结果正常。不过,在使用gcc编译时会提示:
warning: passing argument 3 of 'pthread_create' from incompatible pointer type [-Wincompatible-pointer-types]
expected 'void * (*)(void *)' but argument is of type 'void * (*)(void (*)())'
我想知道代码怎么改能去掉这个提示,望大伙赐教。


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

//回调函数
void OutputSth(const char *str)
{
printf("%s\n", str);
}

//线程函数
void *ThreadRun(void (*func)())
{
const char str[20] = "hello callback.";
int i = 0;
for(; i < 10; ++i)
{
func(str);
sleep(1);
}

return NULL;
}

int main(void)
{
pthread_t thd;
pthread_create(&thd, NULL, ThreadRun, OutputSth);
pthread_join(thd, NULL);

return 0;
}

...全文
1912 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
乞丐的生鱼片 2018-05-02
  • 打赏
  • 举报
回复
线程对应的函数的格式必须是这样:void *xxoo(void *arg) a.把“void *sayhello(void *arg)”改成“void sayhello(void *arg)”会报错如下: root@book-desktop:/opt/pc_test/multithreading/1# make gcc -o main main.c -lpthread main.c: In function ‘main’: main.c:14: 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 (*)(void *)’ b.把“void *sayhello(void *arg)”改成“void *sayhello(void)”会报错如下: root@book-desktop:/opt/pc_test/multithreading/1# make gcc -o main main.c -lpthread main.c: In function ‘main’: main.c:14: 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 * (*)(void)’
toongyu_zty 2018-05-02
  • 打赏
  • 举报
回复
引用 2 楼 cfjtaishan 的回复:
pthread_create第三个参数是void *类型,但是void (*func)()函数的返回值是void类型,与void *冲突。 下面的修改,参考一下吧
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

struct param {
    void (*func)(const char *);
};

//回调函数
void OutputSth(const char *str)
{
    printf("%s\n", str);

    //return NULL;
}

//线程函数
void *ThreadRun(void *arg)
{
    const char str[20] = "hello callback.";
    struct param *p = (struct param *)arg;
    int i = 0;
    for(; i < 10; ++i)
    {
        p->func(str);
        sleep(1);
    }

    return NULL;
}

int main(void)
{
    pthread_t thd;
    struct param arg;
    arg.func = OutputSth;
    pthread_create(&thd, NULL, ThreadRun, (void *)&arg);
    pthread_join(thd, NULL);

    return 0;
}
十分感谢,学习了
自信男孩 2018-04-28
  • 打赏
  • 举报
回复
pthread_create第三个参数是void *类型,但是void (*func)()函数的返回值是void类型,与void *冲突。 下面的修改,参考一下吧
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

struct param {
    void (*func)(const char *);
};

//回调函数
void OutputSth(const char *str)
{
    printf("%s\n", str);

    //return NULL;
}

//线程函数
void *ThreadRun(void *arg)
{
    const char str[20] = "hello callback.";
    struct param *p = (struct param *)arg;
    int i = 0;
    for(; i < 10; ++i)
    {
        p->func(str);
        sleep(1);
    }

    return NULL;
}

int main(void)
{
    pthread_t thd;
    struct param arg;
    arg.func = OutputSth;
    pthread_create(&thd, NULL, ThreadRun, (void *)&arg);
    pthread_join(thd, NULL);

    return 0;
}
paschen 2018-04-28
  • 打赏
  • 举报
回复
void *ThreadRun(void (*func)()) 这个函数的参数类型应该是void* 也就是说是:void *ThreadRun(void * func) 这样的形式

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧