Cancelling a stdin.read?

Lass Safin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Apr 10 01:29:22 PDT 2016


I have a multi-threaded program, one thread drawing to a window 
and handling it, the other handling stdin.

The thread which handles stdin is something like this:
char[] buf;
while(true) {
     readln(buf);
}

The window thread also has to receive close-events from the OS, 
such as when the user pressed Ctrl-Q or whatnot.
The following code is what I used to terminate my program:
import core.runtime : Runtime;
import core.stdc.stdlib : exit;
Runtime.terminate;
exit(0);

However this doesn't work, because Runtime.terminate waits for 
the other threads to terminate before proceeding.

So I thought about replacing my above loop with somethings like 
this:
while(NOTCLOSING) {
     ...
}

But this doesn't work either, because the conditional clause 
first gets read when a single loop finishes.
And my loop won't finish until I give it input via stdin.
This means that I can't use e.g. the close button for closing the 
button, without also writing to stdin.

Because of this, I have to resort to an exit(0) alone, forcing 
the program to terminate without running destructors (static 
destructors are my concern here..) of any kind.

Thus, my question is: Is there any way to cancel the read from 
stdin prematurely from another thread, so that the thread can 
finish?

Or perhaps, just running the shared static destructors alone, so 
that at least they can get run before closing via exit?


More information about the Digitalmars-d-learn mailing list