threadFunktion und KlasseMemberFunktion Problem



  • Hallo, liebe Linux und Unix Gemeinde,

    so spät poste ich immernoch mein Problem, da ich nicht schlafen kann.

    Das Problem ist, wenn ich
    threadFunktion benutze, funktioniert t.getNr() nicht mehr, anderes Wort,
    wenn ich t.getNr() aufrufe, bekomme ich immer den Wert, mit dem im Konstruktur
    initialisiert wird (hier ist 0), und nicht den Wert, den ich erwarte.

    Ohne threadFunktion zu benutzen, funktioniert es dagegen richtig.

    Weiß jemand, wo das Problem liegt ?

    void *threadFunktion( void *arg ); 
    
    class Test 
    { 
    public: 
        Test(); 
        void readAndStoreData(); 
        int getNr();
    private: 
        int nu; 
    };
    
    #include <pthread.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <fcntl.h>
    #include <termios.h>
    #include <iostream.h>
    
    Test::Test()
    {
        nu = 0;
    }
    
    void Test::readAndStoreData()
    {
        for ( int i=0; i<10; i++ )
        {
           nu = i+2;
           cout << " nu = " << nu << endl;
           sleep(1);
        }
    }
    
    int Test::getNr()
    {
        return nu;
    }
    
    void *threadFunktion( void *arg ) 
    { 
        // do was 
        Test myt;
        myt.readAndStoreData();     
        return NULL; 
    }
    
    #include "test.h"
    #include <iostream.h>
    
    int main()
    {
        pthread_t mythread;
        Test t;
    
        if(pthread_create(&mythread,NULL,threadFunktion,NULL))
           abort();
    
        for ( int i=0; i<10; i++ )
        {
            cout << "t.getNr() = " << t.getNr() << endl;
            sleep(1);
        }
    
        if(pthread_join(&mythread,NULL))
           abort();
    
        return 0;
    }
    

    Wenn ich threadFunktion in der Klasse deklariere, wie kann ich den Methoden-Namen als Übergabeparameter pthread_create(&mythread,NULL,threadFunktion,NULL) übergeben ? Hat jemand irgend eine Idee ?

    Vielen Dank und gute Nacht !

    Compilieren:

    g++ main.cpp test.h test.cpp -o test -lpthread

    Aufruf:

    ./test



  • http://www.c-plusplus.net/forum/viewtopic-var-t-is-191729-and-highlight-is-.html
    Doppelpost.

    @rena
    Bitte nur einmal posten, auch wenn ich zugeben muss
    dass ich auch nicht wüsste welches Forum
    (ist aber auch schon spät 😉 )



  • Das problem ist schon erledigt. 😋 😋

    Die Lösung:

    Test t;
    void *threadFunktion( void *arg ) 
    { 
        // do was 
        t.readAndStoreData();     
        return NULL; 
    } 
    
    int main()
    {
        pthread_t mythread;
    
        if(pthread_create(&mythread,NULL,threadFunktion,NULL))
           abort();
    
        for ( int i=0; i<10; i++ )
        {
            cout << "t.getNr() = " << t.getNr() << endl;
            sleep(1);
        }
    
        if(pthread_join(&mythread,NULL))
           abort();
    
        return 0;
    }
    

Anmelden zum Antworten