Menu

My main() finishes before my threads do

Help
Anonymous
2000-12-07
2000-12-23
  • Anonymous

    Anonymous - 2000-12-07

    Hi
    I'm using the Common C++ library to implement the use of threads in my college thesis. Right now I'm just coding some short test programs in order to understand the use of your classes.

    I'm using the following tools :
    egcs-1.1.2-30
    egcs-c++-1.1.2-30
    egcs-objc-1.1.2-30
    glibc-2.1.3-15
    glibc-devel-2.1.3-15
    make-3.78.1-4
    CommonC++-1.2.5
    Red Hat 6.2

    So far I have only checked out bug1.cpp. I've made some modifications to understand its behavior. The problem I'm having is basically that the main() creating the thread finishes execution before the thread does, if I don't call sleep from main. 

    I have tried using the IsRunning() function from the main in order to wait for the thread to finish, but even after the Thread finishes execution, IsRunning() keeps returning true.

    I tried using the SetCancel(THREAD_CANCEL_DISABLED) inside the Initial() function, but the main doesn't wait to finish. When I call delete, the function doesn't hang waiting for the Thread to finish.

    I basically want to know how to tell when a Thread is actually running, so I can wait inside main() for it to finish.

    The two codes are:

    --isRunning--
    class ThreadTest : public Thread
    {
    private:
      void Run() { cout << "Run()..." << endl; };
      void Final() { cout << "Final()..." << endl; delete this; };
    public:
      ThreadTest():Thread(NULL){};
    };
      
    int main()
    {
      ThreadTest *thread = new ThreadTest( );
      cout << "before start()..." << endl;
      thread->Start();
      cout << "after start()..." << endl;

      while( thread != NULL && thread->isRunning() ) ;

      cout << "exiting..." << endl;
    }

    --setCancel--
    class ThreadTest : public Thread
    {
      private:
        void Initial();
        void Run() { cout << "Run()..." << endl; };
        void Final() { cout << "Final()..." << endl; };
      public:
        ThreadTest():Thread(NULL){};
    };
      
    void ThreadTest::Initial()
    {
      cout << "Initial()..." << endl;
      setCancel( THREAD_CANCEL_DISABLED );
    }

    int main()
    {
      ThreadTest *thread = new ThreadTest( );
      cout << "before start()..." << endl;
      thread->Start();
      cout << "after start()..." << endl;
      delete thread;
      cout << "exiting..." << endl;
    }

    Thanks for you time,
        Andres Llanos

     
    • David Sugar

      David Sugar - 2000-12-23

      There is still some wierdness with isRunning().  However, if you do a new to create your subthreads, and then do a delete, that delete will wait the current thread until a Join can be completed.  Hence, you should delete your subthread objects before exiting main...

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.