Boost logo

Boost Users :

Subject: Re: [Boost-users] How to use lock_guard
From: Roman Perepelitsa (roman.perepelitsa_at_[hidden])
Date: 2009-03-10 11:51:48


2009/3/10 Daniele Barzotti <daniele.barzotti_at_[hidden]>

> 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();
>

You should not manually call destructors of automatic (stack allocated)
objects. It's an Undefined Behavior and in practice it usually causes
destructor to be called twice. You might want to put lock_guard in
additional scope.

{
  boost::lock_guard<boost::mutex> lock(_mut);
  // Data received from RS232
  _device.GetData(_buffer);
  // .... CODE ....
} // Destructor of lock is called here.
signal_OnData(); }

Roman Perepelitsa.



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