Menu

More Examples on Using Thread core classes

Help
2002-02-16
2002-02-25
  • prashant patel

    prashant patel - 2002-02-16

    There should be more examples on using thread as being a java programmer i am find it difficult to use threads , the thread example below is what is was trying.

    #ifndef MYTHREAD_H
    #define MYTHREAD_H

    #include "mythread.h"
    #include <iostream>
    #include <cc++/thread.h>

    #ifdef CCXX_NAMESPACES
    using namespace std;
    using namespace ost;
    #endif

    class MyThread : public Thread
    {
    public:
      MyThread();
      ~MyThread();
      void Run();
    };
    #endif

    ---------------------cpp---------------------------

    #include "mythread.h"

    MyThread::MyThread()
    {
       cout << "Constructor" << endl;
    }

    MyThread::~MyThread(){}

    void MyThread::Run()
    {
       cout << "CommonC++ Thread" << endl;
       while(true)
       {
        cout << "Hello Thread Slept Again" << endl;
        Thread::Sleep(1000);
      }
    }

    int main()
    {
      MyThread *test = new MyThread;
      test->Start();
      //MyThread test;
      //test.Start();
      ccxx_sleep(100);

    }

    After referring to the example testthread.cpp in demo dir, i had got the thread running when i placed the ccxx_sleep(100) after constructor and the Start(), Also the thread does not sleep for the desired ms just exists.
    Why does it behave this way?
    How can i run the thread with out theccxx_sleep(100)?
    I read the comments on the c++ constructor about not being able to consturct thread
    right away, why?
    why java threads execute straight away?
    I think we need more examples in section like Thread , Socket and Tcp, so more programmer(from java) new to c++ will find it the refrence to use this lib.

     
    • Anonymous

      Anonymous - 2002-02-19

      I don't know if this will help you, but I noticed that you're missing something in your constructor that should really be there.

      Instead of:

         MyThread::MyThread()
         {
         cout << "Constructor" << endl;
         }

      it should be:

         MyThread::MyThread() : Thread()
         {
         cout << "Constructor" << endl;
         }

      This is how I have it in my code where I have used CommonC++ threads.

      And perhaps you should have the added brackets to the end of this line:

         MyThread *test = new MyThread();

      Probably doesn't make a difference, but you never know.

      Good luck. Let me know if things turn out.

      Marc

       
    • Anonymous

      Anonymous - 2002-02-19

      I don't know if this will help you, but I noticed that you're missing something in your constructor that should really be there.

      Instead of:

         MyThread::MyThread()
         {
         cout << "Constructor" << endl;
         }

      it should be:

         MyThread::MyThread() : Thread()
         {
         cout << "Constructor" << endl;
         }

      This is how I have it in my code where I have used CommonC++ threads.

      And perhaps you should have the added brackets to the end of this line:

         MyThread *test = new MyThread();

      Probably doesn't make a difference, but you never know.

      Good luck. Let me know if things turn out.

      Marc

       
    • prashant patel

      prashant patel - 2002-02-19

      Ok I have figured out how to get the thread to sleep but i still have to insert the call
      to ccxx_sleep(1) other wise the thread wont enter Run() how to i do with out it.
      -------------the new cpp file-------------------

      #include "mythread.h"

      #ifdef    CCXX_NAMESPACES
          using namespace std;
          using namespace ost;
      #endif

      MyThread::MyThread() : Thread(){}

      MyThread::~MyThread(){}

      void MyThread::Run()
      {
          int i = 0;
               while(true)
               {
              cout << "Hello Thread Slept " << ++i << endl;
               Sleep(1000);
         }
      }

      void MyThread::Initial()
      {
          setCancel(THREAD_CANCEL_DISABLED);
      }

      int main()
      {
            MyThread *test = new MyThread;
            test->Start();
            ccxx_sleep(1);
            delete test;
      }

       
    • prashant patel

      prashant patel - 2002-02-25

      After trying a lot of option I am not able to get away with the ccxx_sleep() call tried Semaphore but in vain
      int main()
      {
        Semaphore *smph  = new Semaphore(0);
        MyThread  *test  = new MyThread(smph);
        test->Start(smph);
        smph->Wait();
        //ccxx_sleep(10);
        delete test;
      }
      and hence calling the Post() which the last statement in the constructor but the
      result is the same is there a way other than the ccxx_sleep().

       

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.