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
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; }
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.
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
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