0

I very recently started coding in C++11. Since I understood theoritical part, I thought of experimenting by writing code. This is my first program in C++, what it does is:

I am creating a class which has a thread type member and 2 member functions, of which one is a thread function and the other is a function which starts this thread function. But I am facing the below error. I did a lot of Googling, but none of them helped. I want to spawn 2 threads of "runThread" and they will update the counter. I know the counter is not synchronised, but I will take care of that using std::atomic variable once this error gets resolved.

#include <iostream>
#include <stdio.h>
#include <thread>
#include <vector>

class Application {
public:
    Application(int val): counter(val) { printf("value is %d\n", counter); }
    void start();
    void print();
private:
    void runThread();
    int counter;
    std::vector<std::thread> thr;
};

void Application::runThread() {
    for(int i=0;1<50;i++)
        counter++;
}

void Application::start() {
    //std::thread t1(runThread);
    //std::thread t2(runThread);
    //this->thr.emplace_back(std::thread(&Application::runThread, this)); Tried this, but dint work
    //this->thr.emplace_back(std::thread(&Application::runThread, this));Tried this, but dint work
    thr.emplace_back(std::thread(runThread));
    thr.emplace_back(std::thread(runThread));
}

void Application::print() {
    printf("Counter = %d\n", counter);
}

int main(void)
{
    int a;
    Application app(300);
    app.start();
    std::cin >>a;
}

This is the error

../main.cpp: In member function ‘void Application::start()’:
../main.cpp:27:40: error: invalid use of non-static member function
  thr.emplace_back(std::thread(runThread));
                                        ^
../main.cpp:28:40: error: invalid use of non-static member function
  thr.emplace_back(std::thread(runThread));
                                        ^
subdir.mk:18: recipe for target 'main.o' failed
make: *** [main.o] Error 1
halfer
  • 19,824
  • 17
  • 99
  • 186
Naveen KH
  • 153
  • 2
  • 11

3 Answers3

2

The compiler gives you a hint that the function you are using is non-static and it should. This is because in c++ even though You don't see it every class member function which is not static gets an additional argument which is a this pointer which you might be familiar with, this pointer points at the object that the function then operates upon. Because of that your runThread() function can actually be thought of as runThread(Application* this) and that means that the std::thread constructor does not know what value You want to supply for that parameter and expects you to provide it instead with a static member function that takes zero arguments.

One solution would be to make runThread() static and obtain the instance for example from a global variable (no-no) or actually tell std::thread() constructor what the argument is

std::thread(&Application::runThread, this)
riodoro1
  • 1,246
  • 7
  • 14
0

You need to wrap the call, use a lambda:

std::thread([this](){this->runThread();})

or bind function:

std::bind(&Application::runThread, this)

Running example

Netwave
  • 40,134
  • 6
  • 50
  • 93
  • `std::thread` is smarter than you give it credit for. `std::thread thr(&Application::runThread, this);` will do it; there's no need for that explicit `std::bind`. – Pete Becker Apr 26 '17 at 12:16
0

runThread is a non-static member function, which means it cannot be passed by reference. Instead it must be wrapped.

Replace

thr.emplace_back(std::thread(runThread));

with

thr.emplace_back([this]() {this->runThread();});

See also: C++ Passing pointer to non-static member function

Community
  • 1
  • 1
Nic
  • 337
  • 2
  • 12