Async messages to a thread.
John Colvin
john.loughran.colvin at gmail.com
Mon Jul 29 11:55:08 PDT 2013
On Monday, 29 July 2013 at 16:19:03 UTC, lindenk wrote:
>> void foo(Tid parent) {
>> bool run = true;
>> while(run)
>> {
>> //do_some_blocking_function();
>> receiveTimeout(dur!"msecs"(0),
>> (string s){if(s == "STAHP!") run = false;}
>> );
>> }
>>
>> // clean up
>> send(parent, "done");
>> }
>>
>>
>> void main()
>> {
>> auto tid = spawn(&foo, thisTid);
>>
>> // do stuff / wait for some condition
>>
>> send(tid, "STAHP!");
>> auto msg = receiveOnly!(string)();
>> assert(msg == "done");
>> }
>
> Ah, no I mean, what if do_some_blocking_function blocks for
> some indeterminate amount of time. I would like it to exit even
> when it is currently blocking (as it could be unpredictable
> when it will stop blocking).
The blocking function could start in a new thread and do a
receiveTimeout periodically, or you could make it stateful (if
necessary) and return periodically and be restarted if it hasn't
been asked to stop (which is just a very ugly and rudimentary
version of what you'd want to do with fibers).
Or, if you don't care about making a mess, just kill the thread I
guess.
More information about the Digitalmars-d-learn
mailing list