Re: How do I exhaust a thread's message queue?

2011-04-02 Thread Andrej Mitrovic
It turns out the receiveTimeout technique didn't work well for me. In my case, there's a timer which spawns a high-priority thread calling `foo` several hundred times per second. What I really needed is to exhaust the message queue, and then do some other work after that. So I've added a `shared in

Re: How do I exhaust a thread's message queue?

2011-04-01 Thread Andrej Mitrovic
Actually I have a bug in `foo()`, the switch statement is executed even if I didn't get a message back. Here's a fix: void foo() { int result; bool gotMessage; while (true) { gotMessage = receiveTimeout(1000, (int x) { result = x; }

Re: How do I exhaust a thread's message queue?

2011-04-01 Thread Andrej Mitrovic
Btw disregard that "handle" function, I've used it first but then decided to change `result` directly in the receiveTimeout function. It's better that way.

Re: How do I exhaust a thread's message queue?

2011-04-01 Thread Andrej Mitrovic
I'm good at answering my own questions. :p import std.stdio; import std.concurrency; import core.thread; void main() { auto workThread = spawn(&foo); int delay = 500; int command = 0; while(true) { Thread.sleep( dur!("msecs")( delay += 100 ) ); workThread.send

How do I exhaust a thread's message queue?

2011-04-01 Thread Andrej Mitrovic
Note this is just pseudocode: // worker thread void doWork() { // while there's still stuff in the message queue while (messagesInQueue) { result = receiveOnly!int(); switch(result) { // main thread could be enabling or disabling features this w