0

So I have the following c++ class which stores a std::thread as a member variable, and starts a member function in that separate thread. However, this code only builds in visual studio code (using msys2 12.1.0 as a G++ compiler), and causes errors when I try to build it in visual studio code.

The line that causes the error seems to be: foo_thread = std::thread(threadFunction, this); In visual studio code I get a red underline warning saying "no instance of constructor std::thread::thread" matches the argument list", however the code still compiles and runs fine.

In visual studio I get the same error, and a warning "C3867 'Foo::threadFunction': non-standard syntax; use '&' to create a pointer to member", and the code does not compile. When I try foo_thread = std::thread(&threadFunction, this); the error goes away, however when I try to build I get the error "&: illegal operation on bound member function expression. foo_thread = std::thread(&threadFunction, this); does still compile and run on visual studio code however.

How do I make this section work as desired and able to compile across c++ compilers/ides? It is also worth mentioning I don't seem to get these errors when the function that is handed to the newly spawned thread is not a member function, however I need it to be a member function for this program.

Code:

#include <thread>
#include <windows.h>
#include <iostream>

#define DllExport   __declspec( dllexport )

typedef void (*funcPtr)(int num);

class Foo {
    public:
        // Value Constructor
        Foo(int a, int b, funcPtr f) {
            foo = a;
            bar = b;
            myFunc = f;
        }
        int getFoo() { return foo; };
        int addBar(int a) { return private_method(a); };
        void callMyFunc(int n) { myFunc(n); };
        // Start Monitor
        void StartThread();
        // Stop Monitor
        // IsMonitoring()
        ~Foo() {
            if (foo_thread.joinable()) {
                foo_thread.join();
            }
        };
    private:
        int private_method(int a) { return a + bar; };
        int foo;
        int bar;
        std::thread foo_thread;
        void threadFunction();
        funcPtr myFunc;
        std::atomic<bool> monitoring = ATOMIC_VAR_INIT(false);
};

void Foo::StartThread() {
    foo_thread = std::thread(threadFunction, this);
}

void Foo::threadFunction() {
    for (int i = 0; i < 10; i++) {
        std::cout << "Hello:" << i << std::endl;
        Sleep(500);
    }
}
  • `typedef void (*funcPtr)(int num);` -- This is a typedef of a pointer to a "regular" function. This is not compatible with a pointer to a non-static member function (the signature is different). – PaulMcKenzie Jun 27 '22 at 14:58
  • 1
    The syntax you are looking for is `&Foo::threadFunction`, there is a zillion duplicate questions but I cannot search for them right now. "the code still compiles and runs fine" I somehow doubt it but whatever works for you. – n. m. could be an AI Jun 27 '22 at 14:58
  • Does this answer your question? [Start thread with member function](https://stackoverflow.com/questions/10673585/start-thread-with-member-function) – kotatsuyaki Jun 27 '22 at 15:03

1 Answers1

1

I figured it out, proper syntax to make this work is:

foo_thread = std::thread(&Foo::threadFunction, this);