Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ pthread_create::无法初始化类型为';无效*(*)(无效*)&x27;具有类型为';void*';_C++_Macos_Pthreads - Fatal编程技术网

C++ pthread_create::无法初始化类型为';无效*(*)(无效*)&x27;具有类型为';void*';

C++ pthread_create::无法初始化类型为';无效*(*)(无效*)&x27;具有类型为';void*';,c++,macos,pthreads,C++,Macos,Pthreads,我试图将名称空间函数传递给pthread_create,但编译器给了我错误,我在stackoverflow中搜索过,但无法修复我的问题: #include <pthread.h>

我试图将名称空间函数传递给pthread_create,但编译器给了我错误,我在stackoverflow中搜索过,但无法修复我的问题:

#include <pthread.h>                                                                                                                                                                                                                                                                                             
#include <iostream>
namespace SBProcThreads
{
  void ProcThread(void* defArg)
  {
    std::cout<<"### :"<<__PRETTY_FUNCTION__<<":   ThreadId  :"<<(pthread_self())->__sig<<": ###"<<std::endl;
  }
}


int main()
{
  pthread_t pThreadId;
  ::pthread_create(&pThreadId,NULL,(void*)&SBProcThreads::ProcThread,NULL);
}
#包括
#包括
名称空间SBProcThreads
{
void ProcThread(void*devarg)
{

std::cout您只需声明它,这样它就不需要强制转换:

  void *ProcThread(void* defArg)

  pthread_t pThreadId;
  ::pthread_create(&pThreadId,NULL,SBProcThreads::ProcThread,NULL);
或将其转换为所需的形式:

int main()
{
  pthread_t pThreadId;
  ::pthread_create(&pThreadId,NULL,(void*(*)(void*))&SBProcThreads::ProcThread,NULL);
}

pthread_create的函数签名需要一个返回
void*
的函数。另外,从
pthread_self()
返回的
pthread_t
应该被不透明地处理。下面反映了这两个更改,并在我的计算机上编译:

#include <pthread.h>                                                                                                                                                                                                                                                              
#include <iostream>
namespace SBProcThreads
{
    void * ProcThread(void* defArg)
    {
        std::cout<<"### :"<<__PRETTY_FUNCTION__<<":   ThreadId  :"<<(pthread_self())<<": ###"<<std::endl;
    }
}


int main()
{
      pthread_t pThreadId;
      ::pthread_create(&pThreadId,NULL,SBProcThreads::ProcThread,NULL);
}
#包括
#包括
名称空间SBProcThreads
{
void*ProcThread(void*devarg)
{

std::coutThanks@Andy,我将在我的机器中尝试编译并更新。代码只是我应用程序的一部分。我在main()中有一个独立的while循环。我知道使用框架很容易,但我想了解更多关于pThreads的知识,所以我不介意感到痛苦。