I am trying to implement a Parent and Child class. The Parent class would start a thread and run a routine function. The Child class would override the routine function if needed.This seems perfectly possible but I run into trouble by following answers from this earlier post. As stated in one earlier comment, the thread always invoke the routine function in Parent class.
The following is my code:
// Base.h
#include <cstdint>
#include <cstdbool>
#include <thread>
#include <chrono>
#include <mutex>
#include <functional>
class Base
{
private:
protected:
std::thread routineThread;
uint16_t state = 0;
bool isRoutineRunning = false;
void routine();
public:
Base();
~Base();
void startRoutine();
void stopRoutine();
virtual void outputState();
};
// Base.cpp
#include "Base.h"
#include <stdio.h>
#include <time.h>
Base::Base()
: routineThread()
{
}
Base::~Base()
{
isRoutineRunning = false;
if (routineThread.joinable())
{
routineThread.join();
}
}
void Base::outputState()
{
printf("%ld: base class object state = %d\r\n", time(nullptr), state);
}
void Base::routine()
{
while (true)
{
outputState();
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
void Base::startRoutine()
{
printf("%ld: tried to start routine thread\r\n", time(nullptr));
if (!isRoutineRunning)
{
routineThread = std::thread(&Base::routine, this);
isRoutineRunning = true;
printf("%ld: routine thread started\r\n", time(nullptr));
}
}
void Base::stopRoutine()
{
if (isRoutineRunning)
{
isRoutineRunning = false;
if (routineThread.joinable())
{
routineThread.join();
}
}
}
// Derived.h
#include "Base.h"
class Derived : public Base
{
private:
protected:
public:
Derived(/* args */);
~Derived();
void init();
void outputState() override;
};
// Derived.cpp
#include "Derived.h"
#include <stdio.h>
#include <time.h>
Derived::Derived()
{
}
Derived::~Derived()
{
}
void Derived::init()
{
state = 1;
}
void Derived::outputState()
{
printf("%ld: derived class ojbect state = %d\r\n", time(nullptr), state);
}
// main.cpp
#include "./classes/Base.h"
#include "./classes/Derived.h"
int main()
{
Derived derived;
derived.init();
derived.outputState(); This line seems to work as expected if the following line is commented out
derived.startRoutine();
return 1;
}
The output from test function above is:
1761776592: derived class ojbect state = 1 // This is expected
1761776592: tried to start routine thread
1761776592: routine thread started
// The following output are from virtual function in Parent class while state variable is from Child class
1761776592: base class object state = 1
1761776593: base class object state = 1
1761776594: base class object state = 1
1761776595: base class object state = 1
1761776596: base class object state = 1
Update: the code above were compiled in Eclipse CDT on Ubuntu 24.04
Is there a way to make the thread invoke routine function in Child class?
derived.stopRoutine();before exiting themainfunction and see what happens. See here for a minimal working example.stopRoutineasjoin, which better tells the purpose there, i.e. waiting for the end of the thread (see)