I would recommend the following change to reduce the duration of the locked area: within the lock mutex just *copy* the queued messages in a local variable and empty the queue variable. Then the processing can happen outside of the mutex.