Ok this is the first thread:
void CSharedMemory::start(start_mode mode)
{
bool start_mut_locked = true;
impl->running = true;
impl->mode = mode;
stateMetaStruct* state = impl->proc_state;
boost::interprocess::scoped_lock< boost::interprocess::interprocess_mutex > state_access_lock(state->state_access_mut);
while(impl->running)
{
state->data_written = false;
while(!state->data_written)
{
if(start_mut_locked)
{
// We can now unlock and let other threads to send data.
impl->start_mut.unlock();
start_mut_locked = false;
}
state->state_access_cond.wait(state_access_lock); // wait here upon sharedmemory's state change
boost::interprocess::offset_ptr< stateMetaStruct > s = impl->shm_obj.find< stateMetaStruct >(boost::interprocess::unique_instance).first;
state = s.get();
if(!state->data_written)
{
// Spurious wakeup.
glm_debug("Spurious wakeup.");
}
if(this == state->data_written_by_proccess)
{
state->data_written = false;
glm_debug("Ignoring my proper event.");
}
}
if(impl->running)
{
// Got action from other process.
const interprocess_actions state_action = state->action;
if(DO_STOP == state_action) {
}
else if(DUMP_USERS_REQUEST == state_action) {
impl->stateChangedListener->onDumpUsersRequest();
}
else if(DUMP_USERS_REPLY == state_action) {
}
else {
glm_err("Unexpected state.");
}
}
}
}
The second thread try to send data using this method:
void CSharedMemory::sendDumpUsersRequest()
{
// Ensure shm is started.
boost::mutex::scoped_lock lk(impl->start_mut);
glm_debug("%s", __FUNCTION__);
boost::interprocess::offset_ptr< stateMetaStruct > s = impl->shm_obj.find< stateMetaStruct >(boost::interprocess::unique_instance).first;
stateMetaStruct* state = s.get();
boost::interprocess::scoped_lock< boost::interprocess::interprocess_mutex > state_access_lock(state->state_access_mut);
state->action = DUMP_USERS_REQUEST;
state->data_written = true;
state->data_written_by_proccess = this;
// Send request.
state->state_access_cond.notify_all();
}
The behavior is, the second thread block when trying to acquire the scoped_mutex because the first one is waiting on it.