NaviTrack Tutorial:Creating module:Module structure with thread

From NAMIC Wiki
Jump to: navigation, search
Home < NaviTrack Tutorial:Creating module:Module structure with thread

Classes for thread module

Module class

In a thread module, the only difference from a normal module is a module class, where a thread is implemented.

MyTutorialThreadModule.h

#ifndef __MY_TUTORIAL_THREAD_MODULE_H__
#define __MY_TUTORIAL_THREAD_MODULE_H__ 

#include <OpenTracker/OpenTracker.h>
#include <OpenTracker/dllinclude.h>
#include <OpenTracker/input/SPLModules.h>
#include <OpenTracker/input/MyTutorialThreadSink.h>
#include <OpenTracker/input/MyTutorialThreadSource.h>
#include <string>

namespace ot { 

class OPENTRACKER_API MyTutorialThreadModule : public ThreadModule, public NodeFactory
{
 private:
  int stop;

 protected:
  void run();

 public:
  
  // Constructor and destructor
  MyTutorialThreadModule();
  virtual ~MyTutorialThreadModule();
  
  Node* createNode(const std::string& name,  ot::StringTable& attributes);
  void  pushEvent();
  void  pullEvent() {};
  void  init(StringTable&, ConfigNode *); 

  virtual void  start();
  virtual void  close();
  
 private:
  
  MyTutorialThreadSink*   sink;
  MyTutorialThreadSource* source;
  
  friend class  MyTutorialThreadSink;
  friend class  MyTutorialThreadSource;
};
OT_MODULE(MyTutorialThreadModule);
 

} // end of namespace ot 

#endif // __MY_TUTORIAL_THREAD_MODULE_H__

The differences from MyTutorialModule.h is :

  1. MyTutorialThreadModule inherits ThreadModule instead of Module class.
  2. init(): ThreadModule::init() needs to be called in init() for threading.
  3. run(): a main part of the thread.
  4. start(): a function to start the thread.
  5. close(): a function to stop the thread.


init()

void MyTutorialThreadModule::init(StringTable& attributes, ConfigNode * localTree)
{
  std::cout << "MyTutorialThreadModule::init() is called." << std::endl;
  ThreadModule::init( attributes, localTree );      
  
  std::string strName=attributes.get("name");
  std::cout << "MyTutorialThreadModule::init(): attribute \"name\" is " 
            << strName << std::endl;
}

run()

void MyTutorialThreadModule::run()
{
  std::cout << "MyTutorialThreadModule::run() is called." << std::endl;
  while (stop == 0) {
    sleep(1);
    std::cout << "MyTutorialThreadModule::run(): looping." << std::endl;
  }
}

start()

void MyTutorialThreadModule::start()
{
  std::cout << "MyTutorialThreadModule::start() is called." << std::endl;
  stop = 0;
  if (isInitialized() && source != NULL)
    ThreadModule::start();
}

NOTE that start() function may be called even if the module is not specified in the configuration XML file. Therefore, you should explicitly check if the module is used before calling ThreadModule::start().


close()

void MyTutorialThreadModule::close()
{
  std::cout << "MyTutorialThreadModule::close() is called." << std::endl;
  lock();
  stop = 1;
  unlock();
}

Sink class

Same as MyTutorialSink.

Source class

Same as MyTutorialSource.


Go back to Creating module.