Peeking concurrency messages
Anonymouse
zorael at gmail.com
Sun Jun 23 09:33:27 UTC 2019
Concurrency messages are sent via std.concurrency's send and
receive functions. Receiving a message consumes it, in the sense
that receiving again will catch the next message. The only way of
telling whether there is one waiting is to consume the first and
commit to handling it. In other words, there is currently no way
of checking whether the message box is empty; all the (thread ID
Tid) member variables via which you could tell are private to the
std.concurrency module.
Is there any chance we could get the ability to see if a Tid has
messages waiting?
---
diff --git a/std/concurrency.d b/std/concurrency.d
index c89b6c3b4..65850fd38 100644
--- a/std/concurrency.d
+++ b/std/concurrency.d
@@ -334,6 +334,13 @@ public:
formattedWrite(sink, "Tid(%x)", cast(void*) mbox);
}
+ /**
+ * Returns whether or not this Tid has messages waiting.
+ */
+ bool hasMessages() @safe pure nothrow @nogc
+ {
+ return (mbox.m_localMsgs + mbox.m_sharedBox.length) > 0;
+ }
}
@system unittest
---
void foo()
{
assert(!thisTid.hasMessages);
thisTid.send(123);
assert(thisTid.hasMessages);
int i = receiveOnly!int();
assert(!thisTid.hasMessages);
}
This would be immensely useful to me and I would use it straight
away.
More information about the Digitalmars-d
mailing list