Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <pcl/common/time_trigger.h>
00039 #include <pcl/common/time.h>
00040 #include <iostream>
00041
00043 pcl::TimeTrigger::TimeTrigger (double interval, const callback_type& callback)
00044 : callbacks_ ()
00045 , interval_ (interval)
00046 , quit_ (false)
00047 , running_ (false)
00048 , timer_thread_ ()
00049 , condition_ ()
00050 , condition_mutex_ ()
00051 {
00052 timer_thread_ = boost::thread (boost::bind (&TimeTrigger::thread_function, this));
00053 registerCallback (callback);
00054 }
00055
00057 pcl::TimeTrigger::TimeTrigger (double interval)
00058 : callbacks_ ()
00059 , interval_ (interval)
00060 , quit_ (false)
00061 , running_ (false)
00062 , timer_thread_ ()
00063 , condition_ ()
00064 , condition_mutex_ ()
00065 {
00066 timer_thread_ = boost::thread (boost::bind (&TimeTrigger::thread_function, this));
00067 }
00068
00070 pcl::TimeTrigger::~TimeTrigger ()
00071 {
00072 boost::unique_lock<boost::mutex> lock (condition_mutex_);
00073 quit_ = true;
00074 condition_.notify_all ();
00075 lock.unlock ();
00076
00077 timer_thread_.join ();
00078 }
00079
00081 boost::signals2::connection
00082 pcl::TimeTrigger::registerCallback (const callback_type& callback)
00083 {
00084 boost::unique_lock<boost::mutex> lock (condition_mutex_);
00085 return (callbacks_.connect (callback));
00086 }
00087
00089 void
00090 pcl::TimeTrigger::setInterval (double interval_seconds)
00091 {
00092 boost::unique_lock<boost::mutex> lock (condition_mutex_);
00093 interval_ = interval_seconds;
00094
00095 condition_.notify_all ();
00096 }
00097
00099 void
00100 pcl::TimeTrigger::start ()
00101 {
00102 boost::unique_lock<boost::mutex> lock (condition_mutex_);
00103 if (!running_)
00104 {
00105 running_ = true;
00106 condition_.notify_all ();
00107 }
00108 }
00109
00111 void
00112 pcl::TimeTrigger::stop ()
00113 {
00114 boost::unique_lock<boost::mutex> lock (condition_mutex_);
00115 if (running_)
00116 {
00117 running_ = false;
00118 condition_.notify_all ();
00119 }
00120 }
00121
00123 void
00124 pcl::TimeTrigger::thread_function ()
00125 {
00126 static double time = 0;
00127 while (!quit_)
00128 {
00129 time = getTime ();
00130 boost::unique_lock<boost::mutex> lock (condition_mutex_);
00131 if (!running_)
00132 condition_.wait (lock);
00133 else
00134 {
00135 callbacks_();
00136 double rest = interval_ + time - getTime ();
00137 if (rest > 0.0)
00138 condition_.timed_wait (lock, boost::posix_time::microseconds (static_cast<int64_t> ((rest * 1000000))));
00139 }
00140 }
00141 }
00142