Process-Klasse mit ths als Übergabeparameter



  • Hallo c++-community,

    ich versuche gerade zu Übungszwecken eine Prozess-Klasse zu erstellen. Dabei soll this der Übergabeparameter sein und eine Methode der Klasse die ausgeführte Funktion. Der Aufbau ist so:

    process.h

    class clProcess
        {
            public:
                // constructor and destructor
                clProcess (void);
                virtual         ~clProcess (void);
    
                // methodes
                virtual void*   MainLoop(void* Argv);
    
            private:
                pthread_t       _thread;
        };
    

    process.cpp

    #include    "process.h"
    
    clProcess::clProcess(void){
        // funktioniert noch nicht
        pthread_create(&this->_thread, NULL, this->MainLoop, (void*)this);
    }
    
    clProcess::~clProcess(void){
        if (this->_thread != 0){
            pthread_join(this->_thread, NULL);
        }
    }
    
    void* clProcess::MainLoop(void*){
        // tu etwas
        return NULL;
    }
    

    derzeit beokomme ich noch folgende Fehlermeldung:

    invalid use of non-static member function
    pthread_create(&this->_thread, NULL, this->MainLoop, (void*)this);

    Die Frage ist jetzt natürlich, ob die Übergabe von this überhaupt nötig ist? Problem scheint aber die Übergabe der Methode MainLoop.

    mirrowwinger



  • Warum ist MainLoop eine Memberfunktion?



  • Üblicherweise gibt es eine statische 'sprungbrett'-funktion, welche eben asynchron gestartet wird. Die kann zb den void* wieder in eine this* casten und dann eine std::function aufrufen, welche letztendlich auf die benutzerfunktion zeigt.



  • Ok hab jetzt MainLoop nicht mehr als Memberfunktion:

    process.h

    class clProcess
        {
            public:
                // constructor and destructor
                clProcess (void);
                virtual         ~clProcess (void);
    
            private:
                pthread_t       _thread;
        };
    
    void* MainLoop(void* Argv);
    

    process.cpp

    #include    "process.h"
    
    clProcess::clProcess(void){
        // funktioniert
        pthread_create(&this->_thread, NULL, MainLoop, (void*)this);
    }
    
    clProcess::~clProcess(void){
        if (this->_thread != 0){
            pthread_join(this->_thread, NULL);
        }
    }
    
    void* MainLoop(void*){
        // funktioniert noch nicht
        clProcess _process  =   (clProcess) Argv;
        return NULL;
    }
    

    Allerdings funktioniert das zurück Casten noch nicht. (Ich gebe zu ich habe es mit meinen c-Kenntnissen probiert.



  • Funktioniert nicht - was für eine präzise Aussage. Auch C-Compiler geben schon Fehlermeldungen aus, sollte also nichts unbekanntes für dich sein.

    Und auch in C wird aus einem Structpointer durch Casten nicht plötzlich eine Struct.


Anmelden zum Antworten