task_adaptors.hpp
Go to the documentation of this file.
00001 
00017 #ifndef THREADPOOL_TASK_ADAPTERS_HPP_INCLUDED
00018 #define THREADPOOL_TASK_ADAPTERS_HPP_INCLUDED
00019 
00020 
00021 #include <boost/smart_ptr.hpp>
00022 #include <boost/function.hpp>
00023 #include <boost/thread.hpp>
00024 
00025 
00026 namespace boost { namespace threadpool
00027 {
00028 
00037   typedef function0<void> task_func;
00038 
00039 
00040 
00041 
00051   class prio_task_func
00052   {
00053   private:
00054     unsigned int m_priority;  
00055     task_func m_function;     
00056 
00057   public:
00058     typedef void result_type; 
00059 
00060   public:
00065     prio_task_func(unsigned int const priority, task_func const & function)
00066       : m_priority(priority)
00067       , m_function(function)
00068     {
00069     }
00070 
00073     void operator() (void) const
00074     {
00075       if(m_function)
00076       {
00077         m_function();
00078       }
00079     }
00080 
00085     bool operator< (const prio_task_func& rhs) const
00086     {
00087       return m_priority < rhs.m_priority; 
00088     }
00089 
00090   };  // prio_task_func
00091 
00092 
00093 
00094  
00095 
00096 
00097 
00098 
00107   class looped_task_func
00108   {
00109   private:
00110     function0<bool> m_function;   
00111     unsigned int m_break_s;              
00112     unsigned int m_break_ns;             
00113 
00114   public:
00115     typedef void result_type; 
00116 
00117   public:
00122     looped_task_func(function0<bool> const & function, unsigned int const interval = 0)
00123       : m_function(function)
00124     {
00125       m_break_s  = interval / 1000;
00126       m_break_ns = (interval - m_break_s * 1000) * 1000 * 1000;
00127     }
00128 
00131     void operator() (void) const
00132     {
00133       if(m_function)
00134       {
00135         if(m_break_s > 0 || m_break_ns > 0)
00136         { // Sleep some time before first execution
00137           xtime xt;
00138           xtime_get(&xt, TIME_UTC);
00139           xt.nsec += m_break_ns;
00140           xt.sec += m_break_s;
00141           thread::sleep(xt); 
00142         }
00143 
00144         while(m_function())
00145         {
00146           if(m_break_s > 0 || m_break_ns > 0)
00147           {
00148             xtime xt;
00149             xtime_get(&xt, TIME_UTC);
00150             xt.nsec += m_break_ns;
00151             xt.sec += m_break_s;
00152             thread::sleep(xt); 
00153           }
00154           else
00155           {
00156             thread::yield(); // Be fair to other threads
00157           }
00158         }
00159       }
00160     }
00161 
00162   }; // looped_task_func
00163 
00164 
00165 } } // namespace boost::threadpool
00166 
00167 #endif // THREADPOOL_TASK_ADAPTERS_HPP_INCLUDED
00168 


or_libs
Author(s): raphael
autogenerated on Mon Oct 6 2014 02:53:18