c++ - 封装线程会产生问题

标签 c++ multithreading pthreads

我的 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/

相关文章:

c++ - 如何获取接口(interface) getaddrinfo() (addrinfo) 用于 connect() 的 IP

c++ - 使用 C++ 转置矩阵

java - 多个 Java 线程以及跨多个内核的线程分布控制

java - Guava EventBus Google App Engine 友好吗?

创建一个新的瘦进程、 fork 还是线程?

c++ - 调用弱函数符号和强函数符号?

c++ - 如何创建一个类,其中队列作为数据成员,在 C++ 中存储另一个类的实例

c++ - 将结构传递给 gcc 内置函数以进行原子访问

c - C中的多编写器线程安全队列

C/unix 套接字 : Segmentation fault (core dumbed) due to char pointer(? )