Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members

mitkOOCIODelegate.h

00001 /*=========================================================================
00002 
00003   Program:   3DMed
00004   Date:      $Date: 2014-02-25 18:30:00 +0800 $
00005   Version:   $Version: 4.6.0 $
00006   Copyright: MIPG, Institute of Automation, Chinese Academy of Sciences
00007 
00008 =========================================================================*/
00009 
00010 #ifndef __mitkOoCIODelegate_h
00011 #define __mitkOoCIODelegate_h
00012 
00013 #include "mitkThread.h"
00014 #include "mitkSemaphore.h"
00015 
00016 template <class UserClass>
00017 class mitkOoCIODelegate
00018 {
00019 public:
00020     mitkOoCIODelegate(UserClass* UserObject)
00021     {
00022         m_UserObject=UserObject;
00023         m_IOThreadWorking=false;
00024         m_IOStartSema=new mitkSemaphore;
00025         m_IOFinishSema=new mitkSemaphore;
00026         m_IOThread=new MITK_THREAD_MEMBER(mitkOoCIODelegate,this,_IOThreadEntrance);
00027         m_IOThread->Spawn();
00028         m_IOFunction=NULL;
00029     }
00030     virtual ~mitkOoCIODelegate()
00031     {
00032         m_IOThreadWorking=false;
00033         m_IOStartSema->Post();
00034         m_IOFinishSema->Post();
00035         m_IOThread->Wait();
00036         m_IOThread->Delete();
00037         m_IOFinishSema->Delete();
00038         m_IOStartSema->Delete();        
00039     }
00040     void IOStart(void (UserClass::*IOFunction)()=NULL)
00041     {
00042         if (m_IOThreadWorking) m_IOFinishSema->Wait();
00043         m_IOFunction=IOFunction;
00044         m_IOThreadWorking=true;
00045         m_IOStartSema->Post();
00046     }
00047     void IOWait()
00048     {
00049         if (m_IOThreadWorking)  
00050         {
00051             m_IOFinishSema->Wait();
00052             m_IOThreadWorking=false;
00053         }
00054     }
00055 private:
00056     void _IOThreadEntrance(void *)
00057     {
00058         while(1)
00059         {
00060             m_IOStartSema->Wait();
00061             if (!m_IOThreadWorking) break;
00062             if (m_IOFunction) (m_UserObject->*m_IOFunction)();
00063             m_IOFinishSema->Post();
00064         }
00065 
00066     }
00067     UserClass* m_UserObject;
00068     void (UserClass::*m_IOFunction)();
00069     bool m_IOThreadWorking;
00070     mitkSemaphore *m_IOStartSema,*m_IOFinishSema;
00071     mitkThreadDelegate* m_IOThread;
00072 };
00073 
00074 #endif
00075 

Generated on Tue Feb 25 15:00:37 2014 for MITK (Medical Imaging ToolKit) by  doxygen 1.4.3