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

时间:2017-03-26 17:03:02

标签: 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)

Bueno la cosa es que no entiendo el error la verdadnossésies que debo enviar un apuntador oagregaralgúnatributoalmétododede calcular puntos,o si debo llamar al hilo desde otro lugar debido a que pertenece a la clase, ayuda por favor,gracias。

1 个答案:

答案 0 :(得分:0)

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分配内存,因为你的原始函数是在堆栈上创建线程,并且在创建线程之后,它会被自动释放(因为所有堆栈创建的对象都会这样做)