0

在 pthreads 中为 pthread_create 方法传递函数作为参数的正常方法是

pthread_create(&thread,NULL,func,(void*)arg)

而 func() 被声明/定义为

void* func(void* arg);

但是每当我想在 Visual Studio 2012 中的单独 .cpp 中调用 pthread_create 时,它​​都会出现以下错误,如图所示

视觉工作室中的错误

但是如果我定义函数静态,错误就会消失。

static void* func(void* arg);

有什么建议如何在不使其静态的情况下无错误地传递它?

4

1 回答 1

0

错误消息说这AppendData_Linux是类的成员函数XMLParse并且不能转换为指向.所需的普通(非成员)函数pthread_create的指针。

这是解决方案:

class X {
   void* arg;
public:
   void* func() { ... }

   static void* thunk(void* self) {
     return reinterpret_cast<X*>(self)->func();
   }
};

X obj;
pthread_create(thread, NULL, &X::thunk, reinterpret_cast<void*>(&obj));
于 2014-12-17T14:08:25.870 回答