Non-blocking keyboard input

Christian Köstlin christian.koestlin at gmail.com
Wed Dec 27 14:41:05 UTC 2023


On Wednesday, 27 December 2023 at 05:07:04 UTC, Joe wrote:
> ??????????????????????????????????????? Surely there there is a 
> one liner library solution  for this?
>
> I have a program that spawns a thread for debugging information 
> and uses the keyboard input which allows me to display the 
> information.
>
> If I use getchar or readline then it blocks the thread. This is 
> generally fine because that is all the thread does. The problem 
> is that it also blocks the program termination as the main 
> thread will not exit while the thread is running which is is 
> because it's waiting on keyboard input stuck on getchar or 
> fgetc or whatever.
>
> If I terminate the threads using thread_term then it terminates 
> the program but the program then does not return the return 
> code that it was successfully finished(because it was 
> prematurely terminated by thread_term.
>
> Surely there is some type of peek for keyboards in D? I can't 
> seem to get kbhit or because the library is not included(well, 
> I tried to use one from dmc but it seemed to be invalid. 
> snn.lib IIRC).
>
>
> This shouldn't be hard... yet it is.

One option (not tested) should be to close stdin so that readln 
then returns null or something on eof.

Shutting down threads is always tricky.

It would be great if there would be one or two (perhaps one 
synchronous, one asynchronous) main io-frameworks for dlang (also 
as dub packages) that cover the most common use-cases.


Kind regards,
Christian



More information about the Digitalmars-d-learn mailing list