c++ - 无效使用非静态成员函数pthread

标签 c++ linux multithreading compilation pthreads

当我在ubuntu系统中使用ptrhead在C ++ POO中编译线程时,我遇到了一个问题。这是代码。

#include "CalculatePI.hpp"
CalculatePI::CalculatePI() {
}
CalculatePI::CalculatePI(int squareSize) {
    this->squareSize = squareSize;
}    
CalculatePI::~CalculatePI() {
}
int CalculatePI::throwX() {
    return 1 + rand() % this->squareSize;
}    
int CalculatePI::throwY() {
    return 1 + rand() % this->squareSize;
}    
int CalculatePI::GetCountInSide() {
    return this->countInSide;
}    
int CalculatePI::GetCountOutSide() {
    return this->countOutSide;
}    
int CalculatePI::GetSquareSize() {
    return this->squareSize;
}    
double CalculatePI::getPi() {
    return (4 * (this->countInSide / this->countOutSide));
}    
double CalculatePI::pythagoras(int x, int y) {
    return (sqrt(pow(x, 2) + pow(y, 2)));
}    
void CalculatePI::calculatePoints() {
    int x = this->throwX();
    int y = this->throwY();
    if (pythagoras(x, y)<this->squareSize / 2) {
        this->countInSide++;
    }
    this->countOutSide++;

    cout << "Valor de PI: " << this->getPi() << endl;
}
void CalculatePI::launchThread() {
    pthread_t thread;
    pthread_create(&thread, NULL, calculatePoints, NULL);
}


错误信息

cd '/home/david/NetBeansProjects/Montecarlo'
/usr/bin/make -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: se entra en el directorio '/home/david/NetBeansProjects/Montecarlo'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux/montecarlo
make[2]: se entra en el directorio 

'/home/david/NetBeansProjects/Montecarlo'
mkdir -p build/Debug/GNU-Linux
rm -f "build/Debug/GNU-Linux/CalculatePI.o.d"
g++    -c -g -MMD -MP -MF "build/Debug/GNU-Linux/CalculatePI.o.d" -o build/Debug/GNU-Linux/CalculatePI.o CalculatePI.cpp


CalculatePI.cpp: In member function ‘void CalculatePI::launchThread()’:
CalculatePI.cpp:61:50: error: invalid use of non-static member function
     pthread_create(&thread, NULL, calculatePoints, NULL);
                                                        ^


nbproject/Makefile-Debug.mk:67: fallo en las instrucciones para el objetivo 'build/Debug/GNU-Linux/CalculatePI.o'
make[2]: *** [build/Debug/GNU-Linux/CalculatePI.o] Error 1
make[2]: se sale del directorio '/home/david/NetBeansProjects/Montecarlo'
nbproject/Makefile-Debug.mk:60: fallo en las instrucciones para el objetivo '.build-conf'
make[1]: *** [.build-conf] Error 2
make[1]: se sale del directorio '/home/david/NetBeansProjects/Montecarlo'
nbproject/Makefile-impl.mk:39: fallo en las instrucciones para el objetivo '.build-impl'
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2,, total time: 189ms)


布宜诺斯艾利斯的无可辩驳的错误消息,包括阿古加·阿尔古托·阿瓜多·阿尔法托·德·卡皮尔·蓬图斯,西德博·拉马尔·希拉·德·德·德罗·卢加尔·德比多·阿贝加尔·拉普拉斯, ,格拉西亚斯。

最佳答案

CalculatePI.cpp: In member function ‘void CalculatePI::launchThread()’:
CalculatePI.cpp:61:50: error: invalid use of non-static member function
 pthread_create(&thread, NULL, calculatePoints, NULL);
                                                    ^


pthrad是C库。因此,pthread_create需要一个静态函数。我建议的是传递静态函数并将对象作为参数。

static void CalculatePI::calculatePointsStatic(void *arg) {
  CalculatePI* pObj = (CalculatePI *)arg;
  pObj->calculatePoints();
}

void CalculatePI::calculatePoints() {
  int x = this->throwX();
  int y = this->throwY();
  if (pythagoras(x, y)<this->squareSize / 2) {
      this->countInSide++;
  }
  this->countOutSide++;
  cout << "Valor de PI: " << this->getPi() << endl;
}


并将静态函数传递给pthread_create:

void CalculatePI::launchThread() {
  pthread_t* pthread = new pthread_t;
  pthread_create(pthread, NULL, calculatePointsStatic, this); // pass "this" to the thread function
}


请注意,我正在使用new为pthread_t分配内存,因为您的原始函数正在堆栈上创建线程,并且在创建线程之后,它会自动释放(就像所有堆栈创建的对象一样)

关于c++ - 无效使用非静态成员函数pthread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43031800/

相关文章:

c++ - 需要对特定安全优化的解释

c++ - 我应该删除函数中的局部指针吗? (C++)

linux - 多点触控的解析/dev/input/mouse0

linux - 当列可能存在或可能不存在时,如何获取多列文件的最后 4 个字符?

c# - 从另一个线程更新控件的属性 (label.Text)

c++ - 窗口出现奇怪的 SFML 错误

c++ - 使用移位运算符将 DWORD 转换为字节

linux - 将目录挂载到 docker 容器

c - 线程数组中的第一个线程在 c 中被跳过(有时)?

线程执行时 Python GUI 没有响应