You may wonder how you can use ncurses in an asynchronous fashion in your program. Doesn’t ncurses block on getch(), thus forcing your program out of any of your asnychronous hopes and dreams?
Well, no, you don’t HAVE to use getch() to get characters, and as long as you do not call any of these blocking functions, and find another way to get input in an asynchronous fashion, you can very well use the keyboard event of a character to drive your ncurses gui.
You can also have events happening on other file descriptors at the same time. This is the nature of asynchronous programming—things are happening at the same time, all functions are returning as soon as possible, and nothing is waiting for anything else.
Your program spends most of its time in the “event loop” where it waits for “something to happen” (e.g. data just came in on the standard input, or data just came in on a TCP socket), after which it will fire off the “callback” in which you’ve described what work should be done in the event of such an occurrence.
The following is what the code for this looks like if you were to use poll(). Keep in mind there are other functions, such as select(), that do the same thing, but are used in a slightly different way.
1 |
|
The above is chopped and sliced out of a program I've been working on:
If you are interested in Unix socket and network programming, I highly recommend this resource: