Hi Shiina,

So, I just want to find any example programme, which using the OpenThreads 
library, and It could be very helpful for me.

Well technically, the whole of OSG is an example of OpenThreads... But that might not be useful to you since it's a lot of code to look at.

Essentially, you just need to derive a class from OpenThreads::Thread and override the run() method. In there your thread will do its work.

If your thread needs to always run in the background, the run() method will typically have a while() loop (which can check a variable to see if it needs to stop, which can be set by the cancel() method) and call Thread::microSleep() to yield to other threads when it's done for now. So for example (untested, might not compile but just to give you an example)

class MyThread : public OpenThreads::Thread
{
  public:
    MyThread() : _done(false) { /* ... */ }
    virtual ~MyThread() { /* ... */ }

    virtual void run()
    {
      // do some initialization that might need to be done right before
      // the thread does its work
      while (!_done)
      {
        // do some work
        OpenThreads::Thread::microSleep(1);
      }
      // do some cleanup that might need to be done right after the
      // thread stops working
    }

    /// called from some other thread to tell this thread to stop
    /// working the next time it loops.
    void stopWorking() { _done = true; }

  protected:
    bool _done;
};

Then from some other thread, say the main thread, you would do:

MyThread* myThread = new MyThread;
myThread->start();

// ...

// ... when we're done ...
myThread->stopWorking()
delete myThread;

In your case, you said you want to do something every second, then that's a separate matter, you just need to use osg::Timer to check the current time and do your work each second, perhaps inside the while() above.

Hope this helps get you started,

J-S
--
______________________________________________________
Jean-Sebastien Guay    jean-sebastien.g...@cm-labs.com
                               http://www.cm-labs.com/
                        http://whitestar02.webhop.org/
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to