pthreads错误:类型的参数“ void *( *)()();是不兼容的

发布于 2025-02-09 08:40:01 字数 745 浏览 2 评论 0原文

我不太确定该如何调试,我对线程是全新的,并且正在遵循100%有效的教程,但对我来说不适用:

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

void* sample() {
    printf("Thread sample. \n");
}

int main(void) {
    // Thread Creation
    pthread_t tid; // Creation of thread pointer.
    pthread_create(&tid, NULL, &sample, NULL); // Creation of thread.

    // Waiting for threads to be completed:
    pthread_join(tid, NULL);

    return 0;
}

我收到错误:类型“ void 的参数() ()“与“ void” )类型的参数不兼容(void *)

。 com/watch?v = avyejqusfeu“ rel =“ nofollow noreferrer”> https://www.youtube.com/watch?v=voutyejqusfeu

除了我没有打开文件并在线上删除这些内容(因为 在那里。

它不是

I'm not too sure how to debug this, I am completely new to threads and am following a tutorial which worked 100%, but not for me:

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

void* sample() {
    printf("Thread sample. \n");
}

int main(void) {
    // Thread Creation
    pthread_t tid; // Creation of thread pointer.
    pthread_create(&tid, NULL, &sample, NULL); // Creation of thread.

    // Waiting for threads to be completed:
    pthread_join(tid, NULL);

    return 0;
}

I get the error: argument of type "void ()()" is incompatible with parameter of type "void ()(void *).

All I am doing is following exactly the tutorial from: https://www.youtube.com/watch?v=aVyeJQuSFEU

Except I did not open the file and delete those things on line 317 (since it wasn't there). But VS recognizes the pthread library now.

Any help is sincerely appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我为君王 2025-02-16 08:40:01

i获得错误:类型“ void()()”类型的参数与类型的参数“ void()(void *)。

。代码> void* sample()函数不需要参数,但是您应该以pthread_create的方式定义它。 。

承诺返回void*,但 要解决此

问题该教程可以在其在Visual Studio中添加pthread.h等的方式是正确的,但是在使用pthread_create() 时是错误。最好找到更有权威的东西。

I get the error: argument of type "void ()()" is incompatible with parameter of type "void ()(void *).

The error is telling you exactly the problem: you defined your void* sample() function to take no arguments, but you should define it the way pthread_create expects: void* sample(void *ptr) {...}.

If you don't need the input ptr parameter, you can simply not use it.

P.S. There is an additional bug in your sample() function: it promised to return void*, but doesn't. To fix this, add return NULL;.

P.P.S. The tutorial may be correct in the way it adds pthread.h etc to Visual Studio, but is wrong in its use of pthread_create(). You'll be better off finding something more authoritative.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文