我的 pthread_t
线程有以下封装:
#include <pthread.h>
class Thread
{
public:
void run(const int);
static void *run_helper(void *);
bool startThread(int);
void joinThread();
pthread_t th;
};
其中 run
是我的线程例程,run_helper
如下:
void *Thread::run_helper(int num)
{
return (Thread *)this->run(num);
}
我这样开始我的线程:
bool Thread::startThread(intptr_t arg)
{
return (pthread_create(&this->th, NULL, &run_helper(arg), (void *)(intptr_t)arg));
}
但是当我编译时,出现以下错误:
error: lvalue required as unary ‘&’ operand return (pthread_create(&this->th, NULL, &run_helper(arg), (void *)(intptr_t)arg));
error: ‘this’ is unavailable for static member functions return (Thread *)this->run(num);
尽管尝试过,我似乎无法使这种封装工作。
最佳答案
我认为您的问题可能具体是 &this->th
. &
优先级高于 ->
.也许试试 &(this->th)
.
关于c++ - 封装线程会产生问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36773786/