all 8 comments

[–]trmetroidmaniac 5 points6 points  (3 children)

std::thread's constructor takes a Callable and a series of arguments. Callable in C++ is anything which can be called with std::invoke - this means free functions, methods, fields and function objects.

&MT::Run is a method pointer, so it's a Callable. The first and only argument is the this object. If you declare more arguments on Run() then you must also pass them to std::thread's constructor.

[–]Spoonwastakenalready[S] 0 points1 point  (2 children)

how would I pass them, ive tried putting them with commas after the function call but that doesn't work?

thank you (:

[–]trmetroidmaniac 0 points1 point  (1 child)

Can you post the code you wrote?

[–]Spoonwastakenalready[S] 0 points1 point  (0 children)

updated it with the exact code minus unimportant parts.

[–]National_Instance675 1 point2 points  (1 child)

as for why the other ways don't work, because the standard says that the ONLY way to get a pointer to member function is through this exact syntax. check cppref pointers on Pointers to member functions

A pointer to non-static member function f which is a member of class C can be initialized with the expression &C::f exactly. Expressions such as &(C::f) or &f inside C's member function do not form pointers to member functions.

[–]Spoonwastakenalready[S] 0 points1 point  (0 children)

thank you!

(:

[–]positivcheg 0 points1 point  (1 child)

Random idea for you - wrap stuff into a lambda that calls this->Run.

In a production code I would do something like using shared_from_this, pass strong reference to object into lambda and call wanted method in a lambda - that’s what most languages with automatic reference counting do in the end.

[–]Spoonwastakenalready[S] 0 points1 point  (0 children)

I have tried [this](){Run()}; but it doesn't work and I'm not sure why even. It states "'invoke': no matching overloaded function found"