title: Correct Way to Use a Mutex
body: <p>What I would like to know about the code below, is: Are the use of try catch blocks around method calls good practice. What are the follies in the code below?    </p>
  
  <pre><code>#ifndef TIMER_H
  #define TIMER_H
  
  #include &lt;boost/bind/bind.hpp&gt;
  #include &lt;boost/date_time/posix_time/posix_time.hpp&gt;
  #include &lt;boost/function.hpp&gt;
  #include &lt;boost/thread/thread.hpp&gt;
  #include &lt;boost/thread/recursive_mutex.hpp&gt;
  #include &lt;boost/shared_ptr.hpp&gt;
  #include &lt;boost/make_shared.hpp&gt;
  
  #include &lt;stdint.h&gt;
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  template &lt;uint32_t tMilliSeconds&gt;
  class Timer {
  private:
      static Timer *_instance;
      uint32_t mMilliSeconds;
      boost::mutex mListMutex;
      boost::thread mTimerThread;
      std::vector&lt;boost::function&lt;void()&gt; &gt; mHandlerList;
  
      Timer();
      Timer(const Timer &amp;other);
      Timer &amp;operator=(const Timer &amp;other);
      void Run();
  
  public:
      ~Timer();
      static boost::shared_ptr&lt;Timer&lt;tMilliSeconds&gt; &gt; Instance();
      void AddHandler(boost::function&lt;void()&gt; tmpBoostFunction);
  };
  
  template &lt;uint32_t tMilliSeconds&gt;
  Timer&lt;tMilliSeconds&gt;::Timer() :
      mListMutex() {
      mMilliSeconds = tMilliSeconds;
  
      mTimerThread = boost::thread(
          boost::bind(&amp;Timer&lt;tMilliSeconds&gt;::Run, this));
  }
  
  template &lt;uint32_t tMilliSeconds&gt;
  Timer&lt;tMilliSeconds&gt;::~Timer()  {
      mListMutex.lock();
      try {
          mTimerThread.detach();
      } catch (...) {
          mListMutex.unlock();
      }
      mListMutex.unlock();
  }
  
  template &lt;uint32_t tMilliSeconds&gt;
  boost::shared_ptr&lt;Timer&lt;tMilliSeconds&gt; &gt;
  Timer&lt;tMilliSeconds&gt;::Instance() {
      if (!_instance) {
          _instance = new Timer&lt;tMilliSeconds&gt;();
      }
      return boost::shared_ptr&lt;Timer&lt;tMilliSeconds&gt; &gt;(_instance);
  }
  
  template &lt;uint32_t tMilliSeconds&gt;
  void Timer&lt;tMilliSeconds&gt;::Run() {
      while(true) {
          boost::this_thread::sleep(
              boost::posix_time::milliseconds(mMilliSeconds));
          mListMutex.lock();
          for (std::vector&lt;boost::function&lt;void()&gt; &gt;::iterator vect_it =
              mHandlerList.begin(); vect_it != mHandlerList.end();
              ++vect_it) {
              try {
                  (*vect_it)();
              } catch (...) {
                  mListMutex.unlock();
              }
          }
          mListMutex.unlock();
      }
  }
  
  template &lt;uint32_t tMilliSeconds&gt;
  void Timer&lt;tMilliSeconds&gt;::AddHandler(
      boost::function&lt;void()&gt; tmpBoostFunction) {
  
      mListMutex.lock();
      try {
          mHandlerList.push_back(tmpBoostFunction);
      } catch (...) {
          mListMutex.unlock();
      }
      mListMutex.unlock();
  }
  #endif // TIMER_H
  </code></pre>