Promotional Signals - How to manage the lifetime of objects sent to subscribers? Smart pointers?

I am using boost :: signals2 under Red Hat Enterprise Linux 5.3.

My signal creates a copy of the object and sends it to the pointer to subscribers. This was implemented to ensure thread safety so that the worker thread does not update the row property on the object while reading (perhaps I should reconsider the use of locks?).

In any case, I am worried about several subscribers who are casting a pointer to the copied object in their stream. How can I control the lifetime of an object? How can I find out that all subscribers are satisfied with the object and it is safe to delete the object?

typedef boost::signals2::signal< void ( Parameter* ) > signalParameterChanged_t;
signalParameterChanged_t    m_signalParameterChanged;

// Worker Thread - Raises the signal
void Parameter::raiseParameterChangedSignal()
{
      Parameter* pParameterDeepCopied = new Parameter(*this);
      m_signalParameterChanged(pParameterDeepCopied);
}
// Read-Only Subscriber Thread(s) - GUI (and Event Logging thread ) handles signal
void ClientGui::onDeviceParameterChangedHandler( Parameter* pParameter)
{
      cout << pParameter->toString() << endl;
      delete pParameter;  // **** This only works for a single subscriber !!!
}

Thanks in advance for any advice or guidance,

Red

+3
1

Parameter , boost::shared_ptr:

typedef boost::shared_ptr<Parameter> SharedParameterPtr;
typedef boost::signals2::signal< void ( SharedParameterPtr ) > signalParameterChanged_t;
signalParameterChanged_t    m_signalParameterChanged;

// The signal source
void Parameter::raiseParameterChangedSignal()
{
      SharedParameterPtr pParameterDeepCopied = new Parameter(*this);
      m_signalParameterChanged(pParameterDeepCopied);
}
// The subscriber handler
void ClientGui::onDeviceParameterChangedHandler( SharedParameterPtr pParameter)
{
      cout << pParameter->toString() << endl;
}

, , , (.. ).

, ?

EDIT:

, shared_ptr , / / - . - . , -, .

, raiseParameterChangedSignal() , ? GUI API .

+2

Source: https://habr.com/ru/post/1727640/


All Articles