Replaced instances of std::unique_lock with std::lock_guard where possible

If a lock is used with a mutex that doesn't need to be unlocked again before the lock is destroyed, std::lock_guard can be more efficient than std::unique_lock due to less overhead.

This commit also removes a redundant set of curly braces in PIDController.cpp intended to constrain a lock's scope.

Change-Id: Idd692ce439528ddb319a4c62c40c7351a664eb97
This commit is contained in:
Tyler Veness
2015-09-01 16:47:57 -07:00
committed by Brad Miller (WPI)
parent f64b055499
commit c0ecde302f
20 changed files with 225 additions and 227 deletions

View File

@@ -96,7 +96,7 @@ double Timer::Get() const
double result;
double currentTime = GetFPGATimestamp();
std::unique_lock<priority_mutex> sync(m_mutex);
std::lock_guard<priority_mutex> sync(m_mutex);
if(m_running)
{
// This math won't work if the timer rolled over (71 minutes after boot).
@@ -118,7 +118,7 @@ double Timer::Get() const
*/
void Timer::Reset()
{
std::unique_lock<priority_mutex> sync(m_mutex);
std::lock_guard<priority_mutex> sync(m_mutex);
m_accumulatedTime = 0;
m_startTime = GetFPGATimestamp();
}
@@ -130,7 +130,7 @@ void Timer::Reset()
*/
void Timer::Start()
{
std::unique_lock<priority_mutex> sync(m_mutex);
std::lock_guard<priority_mutex> sync(m_mutex);
if (!m_running)
{
m_startTime = GetFPGATimestamp();
@@ -148,7 +148,7 @@ void Timer::Stop()
{
double temp = Get();
std::unique_lock<priority_mutex> sync(m_mutex);
std::lock_guard<priority_mutex> sync(m_mutex);
if (m_running)
{
m_accumulatedTime = temp;
@@ -168,7 +168,7 @@ bool Timer::HasPeriodPassed(double period)
{
if (Get() > period)
{
std::unique_lock<priority_mutex> sync(m_mutex);
std::lock_guard<priority_mutex> sync(m_mutex);
// Advance the start time by the period.
// Don't set it to the current time... we want to avoid drift.
m_startTime += period;