Boost logo

Boost Users :

Subject: [Boost-users] How to use lock_guard
From: Daniele Barzotti (daniele.barzotti_at_[hidden])
Date: 2009-03-10 06:04:35


Hi,

I'm tring to use lock_guard, but I have some doubts...

I have a method called OnDataReceived() which is called by another class
through a signal.

void ATDispatcher::OnDataReceived()
{
  boost::lock_guard<boost::mutex> lock(_mut);

  // Data received from RS232
  _device.GetData(_buffer);

  // .... CODE ....

  // unlock the mutex before notifications
  lock.~lock_guard();

  signal_OnData();
}

The first time it's called, it works fine.
The second time the flow locks on the creation of the lock object.

In the caller class I have solved this declaring the lock in this manner:

  boost::mutex::scoped_lock lock(_mutex);
  lock.unlock();

  while (true)
  {
     //main loop
      lock.lock();

      // saving data to vector
      _read_buffer.append(buf, rd);

      //Unlock the mutex
      lock.unlock();

      signal_OnData();
  }

but here because there is the worker thread loop, while in the called
class there is no loop.

Thanks for suggestions.
Daniele.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net