Skip to content

Commit

Permalink
[common] Migrate mutex, lock and cvs to C++14. (#3063)
Browse files Browse the repository at this point in the history
[common] Migrate mutex, lock and cvs to C++14.
  • Loading branch information
SergioRAgostinho authored and taketwo committed May 5, 2019
1 parent feb198f commit 9e1ef46
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
2 changes: 0 additions & 2 deletions common/include/pcl/common/boost.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@
#include <boost/mpl/size.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/function.hpp>
//#include <boost/timer.hpp>
#include <boost/thread.hpp>
#include <boost/thread/condition.hpp>
#include <boost/signals2.hpp>
#include <boost/signals2/slot.hpp>
#include <boost/algorithm/string.hpp>
Expand Down
22 changes: 12 additions & 10 deletions common/include/pcl/common/synchronizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

#pragma once

#include <mutex>

namespace pcl
{
/** /brief This template class synchronizes two data streams of different types.
Expand All @@ -50,9 +52,9 @@ namespace pcl
{
typedef std::pair<unsigned long, T1> T1Stamped;
typedef std::pair<unsigned long, T2> T2Stamped;
boost::mutex mutex1_;
boost::mutex mutex2_;
boost::mutex publish_mutex_;
std::mutex mutex1_;
std::mutex mutex2_;
std::mutex publish_mutex_;
std::deque<T1Stamped> queueT1;
std::deque<T2Stamped> queueT2;

Expand All @@ -67,15 +69,15 @@ namespace pcl
int
addCallback (const CallbackFunction& callback)
{
boost::unique_lock<boost::mutex> publish_lock (publish_mutex_);
std::unique_lock<std::mutex> publish_lock (publish_mutex_);
cb_[callback_counter] = callback;
return callback_counter++;
}

void
removeCallback (int i)
{
boost::unique_lock<boost::mutex> publish_lock (publish_mutex_);
std::unique_lock<std::mutex> publish_lock (publish_mutex_);
cb_.erase (i);
}

Expand All @@ -102,8 +104,8 @@ namespace pcl
void
publishData ()
{
boost::unique_lock<boost::mutex> lock1 (mutex1_);
boost::unique_lock<boost::mutex> lock2 (mutex2_);
std::unique_lock<std::mutex> lock1 (mutex1_);
std::unique_lock<std::mutex> lock2 (mutex2_);

for (typename std::map<int, CallbackFunction>::iterator cb = cb_.begin (); cb != cb_.end (); ++cb)
{
Expand All @@ -121,15 +123,15 @@ namespace pcl
publish ()
{
// only one publish call at once allowed
boost::unique_lock<boost::mutex> publish_lock (publish_mutex_);
std::unique_lock<std::mutex> publish_lock (publish_mutex_);

boost::unique_lock<boost::mutex> lock1 (mutex1_);
std::unique_lock<std::mutex> lock1 (mutex1_);
if (queueT1.empty ())
return;
T1Stamped t1 = queueT1.front ();
lock1.unlock ();

boost::unique_lock<boost::mutex> lock2 (mutex2_);
std::unique_lock<std::mutex> lock2 (mutex2_);
if (queueT2.empty ())
return;
T2Stamped t2 = queueT2.front ();
Expand Down
7 changes: 5 additions & 2 deletions common/include/pcl/common/time_trigger.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
#include <boost/function.hpp>
#include <boost/thread.hpp>
#include <boost/signals2.hpp>

#include <condition_variable>
#include <mutex>
#endif

namespace pcl
Expand Down Expand Up @@ -99,7 +102,7 @@ namespace pcl
bool running_;

boost::thread timer_thread_;
boost::condition_variable condition_;
boost::mutex condition_mutex_;
std::condition_variable condition_;
std::mutex condition_mutex_;
};
}
14 changes: 8 additions & 6 deletions common/src/time_trigger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pcl::TimeTrigger::TimeTrigger (double interval)
//////////////////////////////////////////////////////////////////////////////////////////////
pcl::TimeTrigger::~TimeTrigger ()
{
boost::unique_lock<boost::mutex> lock (condition_mutex_);
std::unique_lock<std::mutex> lock (condition_mutex_);
quit_ = true;
condition_.notify_all (); // notify all threads about updated quit_
lock.unlock (); // unlock, to join all threads (needs to be done after notify_all)
Expand All @@ -88,7 +88,7 @@ pcl::TimeTrigger::registerCallback (const callback_type& callback)
void
pcl::TimeTrigger::setInterval (double interval_seconds)
{
boost::unique_lock<boost::mutex> lock (condition_mutex_);
std::unique_lock<std::mutex> lock (condition_mutex_);
interval_ = interval_seconds;
// notify, since we could switch from a large interval to a shorter one -> interrupt waiting for timeout!
condition_.notify_all ();
Expand All @@ -98,7 +98,7 @@ pcl::TimeTrigger::setInterval (double interval_seconds)
void
pcl::TimeTrigger::start ()
{
boost::unique_lock<boost::mutex> lock (condition_mutex_);
std::unique_lock<std::mutex> lock (condition_mutex_);
if (!running_)
{
running_ = true;
Expand All @@ -110,7 +110,7 @@ pcl::TimeTrigger::start ()
void
pcl::TimeTrigger::stop ()
{
boost::unique_lock<boost::mutex> lock (condition_mutex_);
std::unique_lock<std::mutex> lock (condition_mutex_);
if (running_)
{
running_ = false;
Expand All @@ -125,16 +125,18 @@ pcl::TimeTrigger::thread_function ()
while (true)
{
double time = getTime ();
boost::unique_lock<boost::mutex> lock (condition_mutex_);
std::unique_lock<std::mutex> lock (condition_mutex_);
if(quit_)
break;
if (!running_)
condition_.wait (lock); // wait util start is called or destructor is called
else
{
using namespace std::chrono_literals;

callbacks_();
double rest = interval_ + time - getTime ();
condition_.timed_wait (lock, boost::posix_time::microseconds (static_cast<int64_t> ((rest * 1000000))));
condition_.wait_for (lock, rest * 1s);
}
}
}
Expand Down

0 comments on commit 9e1ef46

Please sign in to comment.