So my problem starts pretty much like this question:
Start thread with member function
I have some class Foo, that looks like this:
struct Foo
{
int y;
thread t;
void set(int x){
y = x;
}
void plan(int x){
thread = std::thread ([&]{
set(x);
});
void get(){
if (t.joinable())
t.join();
}
};
Other answers also suggest:
void plan(int x){
thread = std::thread(&Foo::set, this, x);
};
Now, I want to use Foo
as a base class for various child class with overloaded set()
functions, for exemple:
struct Bar: public Foo
{
void set(int x){
y = x*2;
}
}
My problem is that if done that way, Bar::plan()
result in Foo::set()
being runned in the new thread instead of Bar::set
as expected.
Is there another solution than having to write again the plan()
method in every child class of Foo
?