simpler Thread



  • Hi,

    ich bin relativ neu in C++ unter Linux.

    Ich möchte gerade ein Programm mit Threads erstellen.

    Dazu habe ich den Quellcode:

    #include <Thread.h>
    
    class Thread
    {
       public:
          Thread();
          int Start(void * arg);
       protected:
          int Run(void * arg);
          static void * EntryPoint(void*);
          virtual void Setup();
          virtual void Execute(void*);
          void * Arg() const {return Arg_;}
          void Arg(void* a){Arg_ = a;}
       private:
          THREADID ThreadId_;
          void * Arg_;
    
    };
    
    Thread::Thread() {}
    
    int Thread::Start(void * arg)
    {
       Arg(arg); // store user data
       int code = thread_create(Thread::EntryPoint, this, & ThreadId_);
       return code;
    }
    
    int Thread::Run(void * arg)
    {
       Setup();
       Execute( arg );
    }
    
    /*static */
    void * Thread::EntryPoint(void * pthis)
    {
       Thread * pt = (Thread*)pthis;
       pthis->Run( Arg() );
    }
    
    virtual void Thread::Setup()
    {
            // Do any setup here
    }
    
    virtual void Thread::Execute(void* arg)
    {
            // Your code goes here
    }
    int main()
    {
    }
    

    Das Programm macht natürlich noch nichts. Aber ich erhalte beim Kompilieren schon einige Fehlermeldungen:

    thread.cpp:1:20: error: Thread.h: Datei oder Verzeichnis nicht gefunden
    thread.cpp:16: error: 'THREADID' does not name a type
    thread.cpp: In member function 'int Thread::Start(void*)':
    thread.cpp:26: error: 'ThreadId_' was not declared in this scope
    thread.cpp:26: error: 'thread_create' was not declared in this scope
    thread.cpp: In static member function 'static void* Thread::EntryPoint(void*)':
    thread.cpp:40: error: 'void*' is not a pointer-to-object type
    thread.cpp:40: error: cannot call member function 'void* Thread::Arg() const' without object
    thread.cpp: At global scope:
    thread.cpp:43: error: virtual outside class declaration
    thread.cpp:48: error: virtual outside class declaration
    

    Kann mir jemand sagen was ich falsch mache?



  • was genau erwartest du, dass in Thread.h steht?
    die api funktionen für threads unter linux sind in der datei <pthread.h>.



  • Ok vielen Dank.

    Aber ich erhalte trotzdem alle Fehlermeldungen:

    thread.cpp:16: error: 'THREADID' does not name a type
    thread.cpp: In member function 'int Thread::Start(void*)':
    thread.cpp:26: error: 'ThreadId_' was not declared in this scope
    thread.cpp:26: error: 'thread_create' was not declared in this scope
    thread.cpp: In static member function 'static void* Thread::EntryPoint(void*)':
    thread.cpp:40: error: 'void*' is not a pointer-to-object type
    thread.cpp:40: error: cannot call member function 'void* Thread::Arg() const' without object
    thread.cpp: At global scope:
    thread.cpp:43: error: virtual outside class declaration
    thread.cpp:48: error: virtual outside class declaration
    


  • ich glaube, du brauchst http://www.pronix.de/pronix-24.html.


Anmelden zum Antworten