Dieses Problem scheint zu Threads verwandt zu sein und das Programm sehr schnell auszuführen. Ich habe 2 Klassen, ThreadParent
und ThreadChild
, wo man von einem anderen erbt. ThreadParent
erstellt einen Thread und führt die Funktion func
aus, die als statisch deklariert ist, um Zeigerprobleme zu umgehen. Ich möchte jedoch, dass die geerbten Klassen wie ThreadChild
entscheiden, was der Thread genau tut, so ruft func
die virtuelle Funktion Do
.Virtuelle Funktion von Kind Klasse ruft Elternfunktion einmal
Wenn Sie jedoch ein ThreadChild
Objekt erstellen und sofort den Thread ausführen, wird am Anfang ThreadParent::Do
aufgerufen, und alle folgenden Aufrufe sind ThreadChild::Do
. Interessanterweise, wenn ich ein bisschen warte, bevor ich Do
anrufe, passiert es nicht.
Gibt es eine bessere Lösung als ein bisschen zu warten? Und noch wichtiger, warum passiert das?
Hier ist ein kleines, aber vollständiges Beispiel. Es erstellt ein ThreadChild
Objekt, das alle 200ms Do
ausführt. Das Programm endet nach 1s (Warten auf eine Eingabe drücken).
#include <iostream>
#include <windows.h>
#include <thread>
// The parent class
class ThreadParent {
protected:
bool _loopThread; //setting this to false should end the thread
std::thread _thread; //the thread
public:
// Basic constructor
ThreadParent(ThreadParent* child)
: _loopThread(true),
_thread(func, child, &_loopThread) {}
// Stops the thread and waits for it to finish
void StopThread() {
_loopThread = false;
_thread.join();
}
protected:
// The function the thread will be running (static because of pointer issues)
static void func(ThreadParent* child, bool* loopThread) {
//Sleep(10); //<- uncomment to solve the problem?
while (*loopThread) {
child->Do(); // Do is called every 200ms
Sleep(200);
}
}
// The function which is called repeatedly until _loopThread is set to false
virtual void Do() {
std::cout << "Parent call\n";
}
};
// The child class
class ThreadChild : public ThreadParent {
public:
// Basic constructor
ThreadChild()
: ThreadParent(this) {}
protected:
// Redefines Do() with another message
void Do() {
std::cout << "Child call\n";
}
};
// The program
int main() {
ThreadChild thread; // Create and run the thread
Sleep(1000); // Run the thread for 1s
thread.StopThread(); // End it
std::cout << "Press <enter> to terminate...";
std::cin.get(); // Wait for user to end program
return 0;
}
Output:
Parent call
Child call
Child call
Child call
Child call
Press <enter> to terminate...