2

The overall question is simple:

How to gracefully resize a window in a multi-thread ncurses program?


Details here. I have read a few relevant questions (1, 2, 3). From my understanding, there are basically 2 ways to handle a window resize:

  1. Call endwin followed by refresh in user's own SIGWINCH handler.

  2. Leverage ncurses builtin SIGWINCH handler: getch a KEY_RESIZE and handle it.

I have tested on my ncurses implementation to make sure I can receive KEY_RESIZE. But I still don't understand how signal handler works with multi-thread ncurses program:

  1. If I'm using the builtin SIGWINCH handler, which thread will be running the signal handler? Does this matter? Do I have control over this?

  2. The builtin SIGWINCH handler generates a KEY_RESIZE. But what if there are other keys pressed that haven't been delivered? Is KEY_RESIZE guaranteed to be the key returned by the next call of getch?

  3. Handling KEY_RESIZE looks less painful than writing my own SIGWINCH handler. But how can I read this key as soon as it is delivered to make the UI responsive? Can I select or poll stdin to detect its delivery? Is it put on an internal queue that only has something to do with getch but nothing to do with stdin?

  4. If I'm writing my own SIGWINCH handler, do I have any guarantee about ncurses context so that my signal handler code won't break ncurses (by calling functions that cannot be called at the time the signal is delivered, etc.)?

  5. Is ncurses itself single-threaded or multi-threaded? If I block the SIGWINCH signal on all my threads other than the main thread, am I guaranteed to receive the SIGWINCH signal on the main thread (but not an internal thread created by the ncurses library)?

1 Answer 1

2

That's several questions. Briefly:

  • ncurses is single-threaded.

  • ncurses can be configured (build-time) to improve its re-entrancy and add some functions which simplify using mutex's on its structures. But most packagers do not use the feature. A few of the ncurses example programs demonstrate the feature.

  • most implementations of curses (i.e., anything newer than 35 years) lets you call wgetch either nonblocking or with a very short timeout.

  • KEY_RESIZE is treated specially (on the same FIFO as character input, and not affected by the keypad setting, as noted in the wgetch manual page).

  • unless you're using an ncurses configuration with the (rudimentary!) support for threaded applications (and working within its limitations), all of ncurses input/output should be on a single thread. Your other threads can do what they want.

  • ncurses uses a static (persistent/global) variable for recording that a signal arrive. That probably eliminates potential problems with a signal arriving on a different thread than the one which set up the handler. But as suggested here, you may find a bug.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.