exampleThread.cpp

This is an example for the usage of the Thread class and Mutexed Resource. More details about this example.

// ----------------------------------------------------------------------------
//
// $Id$
//
// Copyright 2008, 2009, 2010, 2011, 2012  Antonio Franchi and Paolo Stegagno    
//
// This file is part of MIP.
//
// MIP is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// MIP is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with MIP. If not, see <http://www.gnu.org/licenses/>.
//
// Contact info: antonio.franchi@tuebingen.mpg.de stegagno@diag.uniroma1.it
//
// ----------------------------------------------------------------------------



#include <Thread.h>

using namespace MipBaselib;

class IUseThread{
 private:
  Thread _thread;  
  int _state;
 public:
  IUseThread();
  
  void doWork(){
    _state ++;
   cout << "TID " << pthread_self() << ", I Work " << _state << endl;
  };
  void doClean(){
   _state = 0;
   cout << "TID " << pthread_self() << " I Clean " << endl;
  };
  
  void startWork(){
   _thread.startWork();
  }
  void stopWork(){
   _thread.stopWork();
  }
};

extern "C" void work(void* p){
 IUseThread* IUTp = (IUseThread*) p;
 IUTp->doWork();
}

extern "C" void clean(void* p){
 IUseThread* IUTp = (IUseThread*) p;
 IUTp->doClean();
}

IUseThread::IUseThread() {
 _state = 0;
 /*sets the object that will be passed to scanWork and scanClean*/
 _thread.setWorkObject(this);
 /*sets the routine that must be called after startScan() is called*/
 _thread.setWork(work);
 /*sets the routine that must be called after stopScan() is called*/
 _thread.setWorkClean(clean);
 /*sets the slepping time of the continous scanning thread*/
 _thread.setSleepTime(Time(0,100000));
}


class MutExedResource{
 private:
  EnhancedMutEx _eMutEx;
  /*...*/
 public: 
  /*...*/
  
  /*two mandatory wrappers*/
  bool askExclusiveAccess(Time timeout){
   return _eMutEx.askExclusiveAccess(timeout);
  }
  void leaveExclusiveAccess(){
   _eMutEx.leaveExclusiveAccess();
  }
  
  
  /*tipical interface method*/
  bool getOrPutSomething1(/*input/output parametters*/){
   if(!_eMutEx.allowed()) return false;
   /*use the resource*/
   cout << "you are using me" << endl;
   return true;
  }   
  bool getOrPutSomething2(/*input/output parametters*/){
   if(!_eMutEx.allowed()) return false;
   /*use the resource*/
   cout << "you are using me" << endl;
   return true;
  }
  
  /*...*/
};


class IUseMutExedResource{
 private:
  MutExedResource _mutExedResource;
  /*...*/
 public:
  /*...*/
  
  bool useMuTexedResource(/*input/output parametters*/){
   if(_mutExedResource.askExclusiveAccess(Time(0,100))){ /* ask access */
    _mutExedResource.getOrPutSomething1();
    _mutExedResource.getOrPutSomething2();
    /*use _muTexedResource*/
    _mutExedResource.leaveExclusiveAccess(); /* leave access */
   }
  }
};



int main(int argc,const char* argv[]){

 IUseThread iUseThread;
 for(int i=0; i<2; i++){
  cout << "TID " << pthread_self() << ", I start the work" << endl;
  iUseThread.startWork();
  Timer timer;
  while(timer.get() < Time(2,0)){
   cout << "TID " << pthread_self() << ", I make something else" << endl;
   usleep(500000);
  }
  iUseThread.stopWork();
  cout << "TID " << pthread_self() << ", I've stopped the work" << endl;
 }
 
 return 0;
}



Generated on Mon Feb 20 07:01:06 2017 for MIP by  doxygen 1.5.6