1

I have written a simple class, myshape, with a class method named display_area() that prints area of a rectangle for N number of times where N will be provided by the user. I want this function to run in a thread independently. However while implementing threading I get error saying

error: invalid use of non-static member function  
        std::thread t1(s.display_area, 100);

I have seen the related discussion C++ std::thread and method class! where object instances have been created as a pointer unlike my case and could not able to resolve my problem. I am appending my code below for reference. Any help is appreciated.

#include <iostream>
#include <thread>
using namespace std;

class myshape{
  protected:
    double height;
    double width;
  public:
    myshape(double h, double w) {height = h; width = w;}
    void display_area(int num_loop) {
      for (int i = 0; i < num_loop; i++) {
        cout << "Area: " << height*width << endl;
      }
    }
};

int main(int argc, char** argv) 
{
  myshape s(5, 2);
  s.print_descpirtion();
  std::thread t1(s.display_area, 100);
  t1.join();
}
Geek
  • 395
  • 1
  • 9

1 Answers1

0

First, instances are never "created as a pointer". Sometimes instances are dynamically allocated (and this mechanism gives you a pointer to play with by default). But, even when they're not, they still have an address, and you can still get a pointer that represents that address.

The way we use std::thread's constructor has nothing to do with the storage duration of the object whose member function you wish to call.

So, indeed, you should follow those same instructions:

std::thread t1(&myshape::display_area, &s, 100);

(There's an example of exactly this on cppreference's page for this function.)

As a bonus point of confusion, this constructor also allows you to pass a reference instead of a pointer, so the following will also do fine if you're more comfortable with it:

std::thread t1(&myshape::display_area, s, 100);
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I still get the same error using `std::thread t1(myshape::display_area, s, 100);` – Geek Feb 18 '19 at 02:42
  • @Geek I forgot a `&`. Then, after fixing your `print_descpirtion()` _[sic]_ function not existing, [it works for me](http://coliru.stacked-crooked.com/a/76e9c820cbc28b3c). – Lightness Races in Orbit Feb 18 '19 at 02:44
  • I see that you are compiling with C++17 but I have to compile it using C++11. may be that is reason it does not work for me. For the reference I compile as `g++ -std=c++11 -o test2 test2.cpp`. Any idea? – Geek Feb 18 '19 at 02:50
  • @Geek Here's [the same example in C++11 mode](http://coliru.stacked-crooked.com/a/4f4a3d3e0f1fc6d9). – Lightness Races in Orbit Feb 18 '19 at 02:57
  • Ok it now works on my system using `-pthread` option while compiling. So the total compile command should be `g++ -std=c++11 -o test2 test2.cpp -pthread`. However if I use `gcc` instead of `g++` it throws error. – Geek Feb 18 '19 at 03:18
  • @Geek Why did you do that? – Lightness Races in Orbit Feb 18 '19 at 03:21
  • I wanted to test if I can compile the same code using `gcc -std=c++11 -o test2 test2.cpp -pthread`. – Geek Feb 18 '19 at 03:24