Using lock_guard to handle mutex locking/unlocking through RAII
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Mon, 2 May 2011 11:00:47 +0000 (13:00 +0200)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Mon, 2 May 2011 11:00:47 +0000 (13:00 +0200)
src/link/linkstatusanalyzer.cpp
src/link/linkstatusanalyzer.h

index 8e55a44..f01b6bf 100644 (file)
@@ -7,11 +7,14 @@
 #include <logfunc.hpp>
 
 using namespace std;
+using boost::lock_guard;
+using boost::mutex;
 using boost::posix_time::microsec_clock;
 using boost::posix_time::ptime;
-using boost::mutex;
 using I2n::Logger::GlobalLogger;
 
+typedef lock_guard<mutex> mutex_lock_guard;
+
 //-----------------------------------------------------------------------------
 // LinkStatusAnalyzer
 //-----------------------------------------------------------------------------
@@ -42,7 +45,7 @@ void LinkStatusAnalyzer::notify_host_up( const string &host_address )
 {
     BOOST_ASSERT( !host_address.empty() );
 
-    Mutex.lock();
+    mutex_lock_guard lock( Mutex );
 
     GlobalLogger.info() << "- Host up: " << host_address << endl;
 
@@ -55,15 +58,13 @@ void LinkStatusAnalyzer::notify_host_up( const string &host_address )
 
     // removed from the list?
     BOOST_ASSERT( HostsDownList.count( host_address ) == 0 );
-
-    Mutex.unlock();
 }
 
 void LinkStatusAnalyzer::notify_host_down( const string &host_address )
 {
     BOOST_ASSERT( !host_address.empty() );
 
-    Mutex.lock();
+    mutex_lock_guard lock( Mutex );
 
     GlobalLogger.info() << "- Host down: " << host_address << endl;
 
@@ -76,8 +77,6 @@ void LinkStatusAnalyzer::notify_host_down( const string &host_address )
 
     // inserted in the list?
     BOOST_ASSERT( HostsDownList.count( host_address ) == 1 );
-
-    Mutex.unlock();
 }
 
 void LinkStatusAnalyzer::add_host_up( const string &host_address )
index ffd2930..ab8b1a0 100644 (file)
@@ -5,7 +5,7 @@
 #include <string>
 
 #include <boost/asio.hpp>
-#include <boost/thread/mutex.hpp>
+#include <boost/thread.hpp>
 
 #include "link/statusnotifiercommand.h"