diff --git a/emper/UnboundedBlockingMpscQueue.hpp b/emper/UnboundedBlockingMpscQueue.hpp index c662ded7145da47b83cb33a43be25749a73e5eff..68acb8035d00cbcc8b669e54124daad56e3e0fda 100644 --- a/emper/UnboundedBlockingMpscQueue.hpp +++ b/emper/UnboundedBlockingMpscQueue.hpp @@ -63,9 +63,14 @@ class UnboundedBlockingMpscQueue : public Blockable<LogSubsystem::U_B_MPSC_Q> { if (!tPopped) { Context* context = Context::getCurrentContext(); block([this, context, postRetrieve] { + tryToGetElement(postRetrieve); + + // N.B. It is important that we set blockedContext *after* we + // called tryToGetElement(), as otherwise, multiple threads + // may call tryToGetElement() which results in messages put in + // the queue getting lost. blockedContext = context; - tryToGetElement(postRetrieve); if (tPopped) { tryToWakeupBlockedContext(); }