Thread constructor syntax - passing class members by reference [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Start thread with member function
I have recently been playing around with the new std::thread library in c++11 and I came across a problem. When i try to pass a classes function into a new thread, it gives me an error (I dont have the exact error text right now since im away from home)
I had a class like this
class A
{
void FunctA();
void FunctB();
void run()
{
std::thread t(FunctA);
std::thread r(FunctB);
}
}
What am I doing wrong?

class A
{
void FunctA();
void FunctB();
void run()
{
std::thread t(&A::FunctA, this);
std::thread r(&A::FunctB, this);
}
};
Pointers to member functions are different from pointers to functions, syntax of calling them is different, as well, and requires instance of class. You can just pass pointer to instance as second argument of std::thread constructor.

I think, the problem is that you can't get pointer to member function in a way similar to functions. Here you will find more information about this.
Also, it would be much easier to answer, if you provided compipler error text.

Related

How to make a callback in C++: use a class member function as a parameter [duplicate]

This question already has answers here:
Callback functions in C++
(11 answers)
Closed 2 years ago.
I started using C++ recently and at one point I needed to set up a callback, i.e. call a function of another class and pass to it as a parameter the name of one of my own functions so that it calls it back. At first I tried this:
void myOtherFunction(void (*oneOfMyFunctions)(void)) {
oneOfMyFunctions();
}
Unfortunately this code doesn't support class member functions because it is (correct me if I am wrong) ...C code.
This can work.
void myOtherFunction(void (*oneOfMyFunctions)(void)) {
oneOfMyFunctions();
}
However, your problem may be due to trying to pass member functions into this function. If member_function is a member function of class A, the expression &member_function inside class A has a type of void (A::*)(void), not void (*)(void) like you want (that is, it wants an A pointer in addition to its normal parameters). You can use std::bind():
std::bind(&member_function, this)
to create a function object which can be called with an empty parameter list. However, then you would need to change your member function signature to something like this:
template <typename FuncType>
void myOtherFunction(FuncType oneOfMyFunctions) {
oneOfMyFunctions();
}
or, like Th0rgal may have said,
void myOtherFunction(std::function<void()> oneOfMyFunctions) {
oneOfMyFunctions();
}
Here is a working way to do that:
void myOtherFunction(std::function<void()> oneOfMyFunctions) {
oneOfMyFunctions();
}
And inside my class:
myOtherFunction([&] {
oneOfMyFunctions();
});
Some explanations:
In std::function<void()>, void is what is returned by the function and () contains the types of its parameters (mine is empty because it doesn't have any).
In the 2nd code I am using a lambda to keep the context, as a bind would do (but lambdas replace them).

Thread in C++11 not in class member [duplicate]

This question already has answers here:
Start thread with member function
(5 answers)
Closed 5 years ago.
I'm recently using C++11 threads and I have a question about something (strange for me) happened.
I created a method inside a class, able to start thread. The starting thread method and the callback function of the thread are class methods.
To clarify, in my Person.cpp file I have:
void Person::callbackForThread(){
...
}
void Person::startThread(){
this call threads. Something like:
thread(callbackForThread);
}
The problem is that C++11 doesn't allow me to declare these two function as class methods. This means that if I declare them as normal function, i.e:
void callbackForThread(){
...
}
void startThread(){
this call threads. Something like:
thread(callbackForThread);
}
Everythings works. I would know how could I declare thread and callback inside a class in C++, if it is possible.
For the farther, I have omitted the inclusion of the libraries and the name of the real class. The class shown in this question is fictitious.
This is because Person::callbackForThread takes an hidden first argument: this.
This is where lambdas and std::bind come in handy:
thread([this]() { this->callbackForThread(); });
or
using namespace std::placeholders;
thread(std::bind(&Person::callbackForThread, this, _1));
From krzaq feedback: if thread is std::thread, it needs to be detached or saved somehow.

Passing implicit function pointers [duplicate]

This question already has answers here:
About Pointers To Functions in function declarations
(4 answers)
Closed 7 years ago.
In some C++ 98 code (meaning no, using std::function is not an option), I found the following construct:
class someClass
{
public:
typedef void callback();
void setCallback(callback c)
{
mCallback = c;
}
void callCallback()
{
if (mCallback)
mCallback();
}
private:
callback *mCallback;
};
This confused me. I am used to passing callback functions as a function pointer, so I would expect setCallback to take (*callback)() as argument.
However, the above code seems to work, and compiles without any (related) warnings.
Could someone tell me what is happening here? Is my callback function implicitly passed as a function pointer? Is it a good idea to use this instead of function pointers?
The only thing I could find is that this construction results in "parameter-declaration-clause" ambiguity (C++ 98 8.3p7). Is this the only downside? Are there any benefits?
Similarly to arrays, parameters of function type declare, in fact, a pointer to that type.

How do I pass an instance member function as callback to std::thread [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Start thread with member function
I'm VERY new to C++. My experience has mostly been with javascript and java.
I'm using Xcode on Lion. The following code gives me a compilation error "Reference to non-static member function must be called; did you mean to call it with no arguments?"
class MyClass {
private:
void handler() {
}
public:
void handleThings() {
std::thread myThread(handler);
}
};
I also tried this->handler, &handler, and other variations, but none of them worked. This code compiles though and accomplishes what I want it to:
class MyClass {
private:
void handler() {
}
public:
void handleThings() {
std::thread myThread([this]() {
handler();
});
}
};
Why can't I pass a reference to a member function? Is my work-around the best solution?
std::thread myThread(&MyClass::handler, this);
myThread.join();
Use lambda to access class members within a member function, you need to capture this. [this] is mandatory in below code:
void handleThings()
{
std::thread myThread([this]() {
handler();
});
}
You could capture this by reference but it's not as efficient as capturing it by value. because going through the reference requires double indirection (modulo compiler optimizations)
void handleThings() {
std::thread myThread([&]() {
handler();
});
}
Lambdas are usually a better choice than bind.
Easier for readers to understand.
More efficient.
You can use std::mem_fun, if you dont want to use a lamda.
Are you able to do this?
std::thread myThread(std::mem_fun(&MyClass::handler),this);
std::thread accepts arguments to the function (which is the first argument) and this is passed as an argument to the mem_fun object, which then calls the handler function on this.
You could also quite simply do the below, courtesy - Start thread with member function
std::thread myThread(&MyClass::handler,this);

callback function syntax [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does void(U::*)(void) mean?
Considering the following:
template <class T>
class myButtoncb {
private:
T *ptr;
void (T::*cback) (void)
}
What I understand is:
void (*cback) (void)
Which is nothing but a function pointer that to a function that returns void, and takes no argument.
What I dont understand is, what is the importance of T::? Isn't it enough to declare
only like void (*cback) (void) ?
This says that it's a member function that has a this pointer. Otherwise, it would be a free function, wouldn't have any idea what object it was operating on, and wouldn't be able to access any non-static member functions or member variables.
From C++ FAQ
Is the type of "pointer-to-member-function" different from "pointer-to-function"?
Yep.
Link which I've provided to you has a lot of information about this topic.
The function, you pass there, must be declared inside the class T - the template parameter of myButtoncb. So you can use a function like the following:
class A
{
public:
void foo(void);
};
myButton<A> b;
b.cback = &A::foo;