www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5806] New: std.concurrency : Need the count of a thread's message queue

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5806

           Summary: std.concurrency : Need the count of a thread's message
                    queue
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: andrej.mitrovich gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2011-04-02
11:38:04 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 02 2011
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5806



--- Comment #1 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-01-22
18:04:31 PST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 22 2013