Error: Too few Argument in multithreaded code

Error: Too few Argument in multithreaded code

我一直在尝试实现一个多线程程序,我已经尝试过 并且代码运行良好;但现在由于一些大学项目,我不得不使用 库。 我写了很长的代码,但现在我正在努力解决的问题是关于 pthread_create 函数。 所以,我只写了一部分我要问的代码:

#include<pthread.h>
using namespace std;
void hello() 
{
    cout << "HelloWorld\n";
}
int main()
{
    pthread_t p;
    pthread_create(&p,NULL,hello);
    cout << "Thread created\n";
    return 0;
}

这是我得到的错误:

new.cpp:11:25: error: invalid conversion from ‘void (*)()’ to ‘void* (*)(void*)’ [-fpermissive]
  pthread_create(&p,NULL,hello);
                         ^~~~~
new.cpp:11:30: error: too few arguments to function ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’
  pthread_create(&p,NULL,hello);
                              ^
In file included from /usr/include/x86_64-linux-gnu/c++/8/bits/gthr-default.h:35,
                 from /usr/include/x86_64-linux-gnu/c++/8/bits/gthr.h:148,
                 from /usr/include/c++/8/ext/atomicity.h:35,
                 from /usr/include/c++/8/bits/ios_base.h:39,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from new.cpp:1:
/usr/include/pthread.h:234:12: note: declared here
 extern int pthread_create (pthread_t *__restrict __newthread,
            ^~~~~~~~~~~~~~

我知道可能已经有人问过类似的问题,但我尽可能多地尝试了他们的解决方案并且 none 成功了;另外,我不想将任何参数传递给指定的函数。 不知道现在该怎么办。 感谢任何帮助!!!

这里有两个问题。首先,由 pthread_create 的第三个参数指定的作为线程入口点的函数必须接受 void * 作为参数和 return void *。您的函数必须符合该签名。您可以随意忽略该参数,只需 return NULL.

void *hello(void *param) 
{
    cout << "HelloWorld\n";
    return NULL;
}

其次,pthread_create 接受类型为 void * 的第四个参数,它是线程函数的参数。由于您没有使用参数,因此可以传递一个空指针。

pthread_create(&p,NULL,hello,NULL);