How do I exhaust a thread's message queue?
Andrej Mitrovic
andrej.mitrovich at gmail.com
Fri Apr 1 19:56:21 PDT 2011
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(++command);
}
}
void handle(int x)
{
writeln(x);
}
void foo()
{
int result;
bool gotMessage;
do
{
gotMessage = receiveTimeout(1000,
(int x) { result = x; }
);
switch (result)
{
case 1:
writeln("one");
break;
case 2:
writeln("two");
break;
default:
writeln("something else");
break;
}
} while (gotMessage);
writeln("Done!");
}
I love how elegant D is.
More information about the Digitalmars-d-learn
mailing list