Bug 5806 – std.concurrency : Need the count of a thread's message queue

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2011-04-02T11:38:00Z
Last change time
2016-08-27T21:36:21Z
Assigned to
nobody
Creator
andrej.mitrovich

Comments

Comment #0 by andrej.mitrovich — 2011-04-02T11:38:04Z
Currently I know of no public function from std.concurrency which lets me know if there are *any* messages left in a queue for a thread. My current workaround for this is to have a shared counter, which I increment in my main thread, and decrement in my work thread whenever there are 1 or more messages left in the queue. Having the message queue length known is useful in cases where a work function is constantly being called via a timer function. The work function first checks if the main thread has sent any commands via messages (for example "switch to verbose mode"), and then proceeds to do its own work based on these commands. Here's an example project: http://dl.dropbox.com/u/9218759/threadCount.zip Obviously there's some contention between the main thread and worker thread's stdout, but never mind that. The key here is that I need a way of knowing if there are any messages left in the queue. This example doesn't show a *useful* showcase, but I do have an example of processing MIDI with a high-priority thread which calls a function hundreds of times per second (adapted from PortMidi). I've tried using receiveTimeout, but this can be very very tricky (unreliable) with timers that call a function hundreds of times per second.
Comment #1 by andrej.mitrovich — 2013-01-22T18:04:31Z
Welp, I don't think I know how to implement this, I'm not sure which one of these is right: return m_localMsgs; return m_localBox.length; return m_sharedBox.length; Or whether there should be a synchronized(m_lock) around them. A count might not even be necessary, but I would like to have a 'hasMessages' property in a Tid to know whether to call `receive` or continue doing some unrelated work in the function.
Comment #2 by andrej.mitrovich — 2016-08-27T21:36:21Z
This might make it a leaking API this way. `get` already returns false if the message queue is empty which IIRC resolves my initial problem.