I would like to serialize multi-threaded calls to the boost :: signals2 signal to make sure that notifications of state changes from the object arrive in the slots in a well-defined order.
Background
I have an object with an internal state in a multithreaded program. Some parts of the internal state are interesting for other parts of the program, and the object provides state changes using the boost :: signals2 signal, similar to this:
class ObjectWithState {
public:
enum State {
STATE_A,
STATE_B,
STATE_C,
};
void OnEvent() {
State newState;
{
boost::lock_guard<boost::mutex> lock(m_Mutex);
m_State = ...;
newState = m_State;
}
m_OnStateChanged(newState);
}
private:
boost::signals2::signal<void (State) > m_OnStateChanged;
boost::mutex m_Mutex;
State m_State;
};
Problem
OnEvent, , , . , , . , , . , , , .
- , . , . , .
:
class ObjectWithState {
public:
enum State {
STATE_A,
STATE_B,
STATE_C,
};
void OnEvent() {
State newState;
boost::unique_future<void> waitForPrevious;
boost::shared_ptr<boost::promise<void> > releaseNext;
{
boost::lock_guard<boost::mutex> lock(m_Mutex);
m_State = ...;
newState = m_State;
waitForPrevious = m_CurrentInvocation->get_future();
m_CurrentInvocation.reset(new boost::promise<void>());
releaseNext = m_CurrentInvocation;
}
waitForPrevious.get();
OnStateChanged(newState);
releaseNext->set_value();
}
private:
boost::signals2::signal<void (State) > m_OnStateChanged;
boost::mutex m_Mutex;
State m_State;
boost::shared_ptr<boost::promise<void> > m_CurrentInvocation;
};
, , , . , , , ...:) , :
boost:: signal2 (, 2 )?